blob: 643866b4efad3906812be334cee35a69da1a4d02 [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;
2191
2192 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2193 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2194 bool moved = deltaX != 0 || deltaY != 0;
2195
Jeff Brown65fd2512011-08-18 11:20:58 -07002196 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002197 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2198 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002199 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002200 }
2201
Jeff Brown65fd2512011-08-18 11:20:58 -07002202 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002203 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002204 pointerProperties.clear();
2205 pointerProperties.id = 0;
2206 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2207
Jeff Brown6328cdc2010-07-29 18:18:33 -07002208 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002209 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002210
Jeff Brown65fd2512011-08-18 11:20:58 -07002211 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2212 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002213 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002214
Jeff Brownbe1aa822011-07-27 16:04:54 -07002215 mWheelYVelocityControl.move(when, NULL, &vscroll);
2216 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002217
Jeff Brownbe1aa822011-07-27 16:04:54 -07002218 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002219
Jeff Brownbe1aa822011-07-27 16:04:54 -07002220 if (mPointerController != NULL) {
2221 if (moved || scrolled || buttonsChanged) {
2222 mPointerController->setPresentation(
2223 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002224
Jeff Brownbe1aa822011-07-27 16:04:54 -07002225 if (moved) {
2226 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002227 }
2228
Jeff Brownbe1aa822011-07-27 16:04:54 -07002229 if (buttonsChanged) {
2230 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002231 }
Jeff Brownefd32662011-03-08 15:13:06 -08002232
Jeff Brownbe1aa822011-07-27 16:04:54 -07002233 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002234 }
2235
Jeff Brownbe1aa822011-07-27 16:04:54 -07002236 float x, y;
2237 mPointerController->getPosition(&x, &y);
2238 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2239 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2240 } else {
2241 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2242 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2243 }
2244
2245 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002246
Jeff Brown56194eb2011-03-02 19:23:13 -08002247 // Moving an external trackball or mouse should wake the device.
2248 // We don't do this for internal cursor devices to prevent them from waking up
2249 // the device in your pocket.
2250 // TODO: Use the input device configuration to control this behavior more finely.
2251 uint32_t policyFlags = 0;
2252 if (getDevice()->isExternal()) {
2253 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2254 }
2255
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002256 // Synthesize key down from buttons if needed.
2257 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2258 policyFlags, lastButtonState, currentButtonState);
2259
2260 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002261 if (downChanged || moved || scrolled || buttonsChanged) {
2262 int32_t metaState = mContext->getGlobalMetaState();
2263 int32_t motionEventAction;
2264 if (downChanged) {
2265 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2266 } else if (down || mPointerController == NULL) {
2267 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2268 } else {
2269 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2270 }
Jeff Brownb6997262010-10-08 22:31:17 -07002271
Jeff Brownbe1aa822011-07-27 16:04:54 -07002272 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2273 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002274 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002275 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002276
Jeff Brownbe1aa822011-07-27 16:04:54 -07002277 // Send hover move after UP to tell the application that the mouse is hovering now.
2278 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2279 && mPointerController != NULL) {
2280 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2281 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2282 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2283 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2284 getListener()->notifyMotion(&hoverArgs);
2285 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002286
Jeff Brownbe1aa822011-07-27 16:04:54 -07002287 // Send scroll events.
2288 if (scrolled) {
2289 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2290 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2291
2292 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2293 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2294 AMOTION_EVENT_EDGE_FLAG_NONE,
2295 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2296 getListener()->notifyMotion(&scrollArgs);
2297 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002298 }
Jeff Browna032cc02011-03-07 16:56:21 -08002299
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002300 // Synthesize key up from buttons if needed.
2301 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2302 policyFlags, lastButtonState, currentButtonState);
2303
Jeff Brown65fd2512011-08-18 11:20:58 -07002304 mCursorMotionAccumulator.finishSync();
2305 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002306}
2307
Jeff Brown83c09682010-12-23 17:50:18 -08002308int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002309 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2310 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2311 } else {
2312 return AKEY_STATE_UNKNOWN;
2313 }
2314}
2315
Jeff Brown05dc66a2011-03-02 14:41:58 -08002316void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002317 if (mPointerController != NULL) {
2318 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2319 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002320}
2321
Jeff Brown6d0fec22010-07-23 21:28:06 -07002322
2323// --- TouchInputMapper ---
2324
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002325TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002326 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002327 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002328 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002329}
2330
2331TouchInputMapper::~TouchInputMapper() {
2332}
2333
2334uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002335 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002336}
2337
2338void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2339 InputMapper::populateDeviceInfo(info);
2340
Jeff Brown65fd2512011-08-18 11:20:58 -07002341 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2342 info->addMotionRange(mOrientedRanges.x);
2343 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002344 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002345
Jeff Brown65fd2512011-08-18 11:20:58 -07002346 if (mOrientedRanges.haveSize) {
2347 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002348 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002349
2350 if (mOrientedRanges.haveTouchSize) {
2351 info->addMotionRange(mOrientedRanges.touchMajor);
2352 info->addMotionRange(mOrientedRanges.touchMinor);
2353 }
2354
2355 if (mOrientedRanges.haveToolSize) {
2356 info->addMotionRange(mOrientedRanges.toolMajor);
2357 info->addMotionRange(mOrientedRanges.toolMinor);
2358 }
2359
2360 if (mOrientedRanges.haveOrientation) {
2361 info->addMotionRange(mOrientedRanges.orientation);
2362 }
2363
2364 if (mOrientedRanges.haveDistance) {
2365 info->addMotionRange(mOrientedRanges.distance);
2366 }
2367
2368 if (mOrientedRanges.haveTilt) {
2369 info->addMotionRange(mOrientedRanges.tilt);
2370 }
2371
2372 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2373 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2374 }
2375 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2376 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2377 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002378 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002379}
2380
Jeff Brownef3d7e82010-09-30 14:33:04 -07002381void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002382 dump.append(INDENT2 "Touch Input Mapper:\n");
2383 dumpParameters(dump);
2384 dumpVirtualKeys(dump);
2385 dumpRawPointerAxes(dump);
2386 dumpCalibration(dump);
2387 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002388
Jeff Brownbe1aa822011-07-27 16:04:54 -07002389 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2390 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2391 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2392 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2393 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2394 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002395 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2396 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002397 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002398 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2399 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002400 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2401 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2402 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2403 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2404 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002405
Jeff Brownbe1aa822011-07-27 16:04:54 -07002406 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002407
Jeff Brownbe1aa822011-07-27 16:04:54 -07002408 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2409 mLastRawPointerData.pointerCount);
2410 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2411 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2412 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2413 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002414 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2415 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002416 pointer.id, pointer.x, pointer.y, pointer.pressure,
2417 pointer.touchMajor, pointer.touchMinor,
2418 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002419 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002420 pointer.toolType, toString(pointer.isHovering));
2421 }
2422
2423 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2424 mLastCookedPointerData.pointerCount);
2425 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2426 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2427 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2428 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2429 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002430 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2431 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002432 pointerProperties.id,
2433 pointerCoords.getX(),
2434 pointerCoords.getY(),
2435 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2436 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2437 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2438 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2439 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2440 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002441 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002442 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2443 pointerProperties.toolType,
2444 toString(mLastCookedPointerData.isHovering(i)));
2445 }
2446
Jeff Brown65fd2512011-08-18 11:20:58 -07002447 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002448 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2449 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002450 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002451 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002452 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002453 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002454 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002455 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002456 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002457 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2458 mPointerGestureMaxSwipeWidth);
2459 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002460}
2461
Jeff Brown65fd2512011-08-18 11:20:58 -07002462void TouchInputMapper::configure(nsecs_t when,
2463 const InputReaderConfiguration* config, uint32_t changes) {
2464 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002465
Jeff Brown474dcb52011-06-14 20:22:50 -07002466 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002467
Jeff Brown474dcb52011-06-14 20:22:50 -07002468 if (!changes) { // first time only
2469 // Configure basic parameters.
2470 configureParameters();
2471
Jeff Brown65fd2512011-08-18 11:20:58 -07002472 // Configure common accumulators.
2473 mCursorScrollAccumulator.configure(getDevice());
2474 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002475
2476 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002477 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002478
2479 // Prepare input device calibration.
2480 parseCalibration();
2481 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002482 }
2483
Jeff Brown474dcb52011-06-14 20:22:50 -07002484 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002485 // Update pointer speed.
2486 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2487 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2488 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002489 }
Jeff Brown8d608662010-08-30 03:02:23 -07002490
Jeff Brown65fd2512011-08-18 11:20:58 -07002491 bool resetNeeded = false;
2492 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
2493 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT))) {
2494 // Configure device sources, surface dimensions, orientation and
2495 // scaling factors.
2496 configureSurface(when, &resetNeeded);
2497 }
2498
2499 if (changes && resetNeeded) {
2500 // Send reset, unless this is the first time the device has been configured,
2501 // in which case the reader will call reset itself after all mappers are ready.
2502 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002503 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002504}
2505
Jeff Brown8d608662010-08-30 03:02:23 -07002506void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002507 // Use the pointer presentation mode for devices that do not support distinct
2508 // multitouch. The spot-based presentation relies on being able to accurately
2509 // locate two or more fingers on the touch pad.
2510 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2511 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002512
Jeff Brown538881e2011-05-25 18:23:38 -07002513 String8 gestureModeString;
2514 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2515 gestureModeString)) {
2516 if (gestureModeString == "pointer") {
2517 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2518 } else if (gestureModeString == "spots") {
2519 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2520 } else if (gestureModeString != "default") {
2521 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2522 }
2523 }
2524
Jeff Brownace13b12011-03-09 17:39:48 -08002525 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2526 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2527 // The device is a cursor device with a touch pad attached.
2528 // By default don't use the touch pad to move the pointer.
2529 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002530 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2531 // The device is a pointing device like a track pad.
2532 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2533 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2534 // The device is a touch screen.
2535 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002536 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002537 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002538 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2539 }
2540
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002541 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002542 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2543 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002544 if (deviceTypeString == "touchScreen") {
2545 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002546 } else if (deviceTypeString == "touchPad") {
2547 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002548 } else if (deviceTypeString == "pointer") {
2549 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002550 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002551 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2552 }
2553 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002554
Jeff Brownefd32662011-03-08 15:13:06 -08002555 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002556 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2557 mParameters.orientationAware);
2558
Jeff Brownbc68a592011-07-25 12:58:12 -07002559 mParameters.associatedDisplayId = -1;
2560 mParameters.associatedDisplayIsExternal = false;
2561 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002562 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002563 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2564 mParameters.associatedDisplayIsExternal =
2565 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2566 && getDevice()->isExternal();
2567 mParameters.associatedDisplayId = 0;
2568 }
Jeff Brown8d608662010-08-30 03:02:23 -07002569}
2570
Jeff Brownef3d7e82010-09-30 14:33:04 -07002571void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002572 dump.append(INDENT3 "Parameters:\n");
2573
Jeff Brown538881e2011-05-25 18:23:38 -07002574 switch (mParameters.gestureMode) {
2575 case Parameters::GESTURE_MODE_POINTER:
2576 dump.append(INDENT4 "GestureMode: pointer\n");
2577 break;
2578 case Parameters::GESTURE_MODE_SPOTS:
2579 dump.append(INDENT4 "GestureMode: spots\n");
2580 break;
2581 default:
2582 assert(false);
2583 }
2584
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002585 switch (mParameters.deviceType) {
2586 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2587 dump.append(INDENT4 "DeviceType: touchScreen\n");
2588 break;
2589 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2590 dump.append(INDENT4 "DeviceType: touchPad\n");
2591 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002592 case Parameters::DEVICE_TYPE_POINTER:
2593 dump.append(INDENT4 "DeviceType: pointer\n");
2594 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002595 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002596 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002597 }
2598
Jeff Brown65fd2512011-08-18 11:20:58 -07002599 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2600 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002601 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2602 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002603}
2604
Jeff Brownbe1aa822011-07-27 16:04:54 -07002605void TouchInputMapper::configureRawPointerAxes() {
2606 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002607}
2608
Jeff Brownbe1aa822011-07-27 16:04:54 -07002609void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2610 dump.append(INDENT3 "Raw Touch Axes:\n");
2611 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2612 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2613 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2614 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2615 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2616 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2617 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2618 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2619 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002620 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2621 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002622 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2623 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002624}
2625
Jeff Brown65fd2512011-08-18 11:20:58 -07002626void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2627 int32_t oldDeviceMode = mDeviceMode;
2628
2629 // Determine device mode.
2630 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2631 && mConfig.pointerGesturesEnabled) {
2632 mSource = AINPUT_SOURCE_MOUSE;
2633 mDeviceMode = DEVICE_MODE_POINTER;
2634 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2635 && mParameters.associatedDisplayId >= 0) {
2636 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2637 mDeviceMode = DEVICE_MODE_DIRECT;
2638 } else {
2639 mSource = AINPUT_SOURCE_TOUCHPAD;
2640 mDeviceMode = DEVICE_MODE_UNSCALED;
2641 }
2642
Jeff Brown9626b142011-03-03 02:09:54 -08002643 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002644 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002645 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2646 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002647 mDeviceMode = DEVICE_MODE_DISABLED;
2648 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002649 }
2650
Jeff Brown65fd2512011-08-18 11:20:58 -07002651 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002652 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002653 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002654 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002655 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2656 &mAssociatedDisplayOrientation)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002657 LOGI(INDENT "Touch device '%s' could not query the properties of its associated "
2658 "display %d. The device will be inoperable until the display size "
2659 "becomes available.",
2660 getDeviceName().string(), mParameters.associatedDisplayId);
2661 mDeviceMode = DEVICE_MODE_DISABLED;
2662 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002663 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002664 }
2665
Jeff Brown65fd2512011-08-18 11:20:58 -07002666 // Configure dimensions.
2667 int32_t width, height, orientation;
2668 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2669 width = mAssociatedDisplayWidth;
2670 height = mAssociatedDisplayHeight;
2671 orientation = mParameters.orientationAware ?
2672 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2673 } else {
2674 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2675 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2676 orientation = DISPLAY_ORIENTATION_0;
2677 }
2678
2679 // If moving between pointer modes, need to reset some state.
2680 bool deviceModeChanged;
2681 if (mDeviceMode != oldDeviceMode) {
2682 deviceModeChanged = true;
2683
2684 if (mDeviceMode == DEVICE_MODE_POINTER) {
2685 if (mPointerController == NULL) {
2686 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2687 }
2688 } else {
2689 mPointerController.clear();
2690 }
2691
2692 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002693 }
2694
Jeff Brownbe1aa822011-07-27 16:04:54 -07002695 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002696 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002697 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002698 }
2699
Jeff Brownbe1aa822011-07-27 16:04:54 -07002700 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002701 if (sizeChanged || deviceModeChanged) {
2702 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
2703 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002704
Jeff Brownbe1aa822011-07-27 16:04:54 -07002705 mSurfaceWidth = width;
2706 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002707
Jeff Brown8d608662010-08-30 03:02:23 -07002708 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002709 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2710 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2711 mXPrecision = 1.0f / mXScale;
2712 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002713
Jeff Brownbe1aa822011-07-27 16:04:54 -07002714 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002715 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002716 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002717 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002718
Jeff Brownbe1aa822011-07-27 16:04:54 -07002719 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002720
Jeff Brown8d608662010-08-30 03:02:23 -07002721 // Scale factor for terms that are not oriented in a particular axis.
2722 // If the pixels are square then xScale == yScale otherwise we fake it
2723 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002724 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002725
Jeff Brown8d608662010-08-30 03:02:23 -07002726 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002727 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002728
Jeff Browna1f89ce2011-08-11 00:05:01 -07002729 // Size factors.
2730 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2731 if (mRawPointerAxes.touchMajor.valid
2732 && mRawPointerAxes.touchMajor.maxValue != 0) {
2733 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2734 } else if (mRawPointerAxes.toolMajor.valid
2735 && mRawPointerAxes.toolMajor.maxValue != 0) {
2736 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2737 } else {
2738 mSizeScale = 0.0f;
2739 }
2740
Jeff Brownbe1aa822011-07-27 16:04:54 -07002741 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002742 mOrientedRanges.haveToolSize = true;
2743 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002744
Jeff Brownbe1aa822011-07-27 16:04:54 -07002745 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002746 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002747 mOrientedRanges.touchMajor.min = 0;
2748 mOrientedRanges.touchMajor.max = diagonalSize;
2749 mOrientedRanges.touchMajor.flat = 0;
2750 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002751
Jeff Brownbe1aa822011-07-27 16:04:54 -07002752 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2753 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002754
Jeff Brownbe1aa822011-07-27 16:04:54 -07002755 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002756 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002757 mOrientedRanges.toolMajor.min = 0;
2758 mOrientedRanges.toolMajor.max = diagonalSize;
2759 mOrientedRanges.toolMajor.flat = 0;
2760 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002761
Jeff Brownbe1aa822011-07-27 16:04:54 -07002762 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2763 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002764
2765 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002766 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002767 mOrientedRanges.size.min = 0;
2768 mOrientedRanges.size.max = 1.0;
2769 mOrientedRanges.size.flat = 0;
2770 mOrientedRanges.size.fuzz = 0;
2771 } else {
2772 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002773 }
2774
2775 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002776 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002777 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2778 || mCalibration.pressureCalibration
2779 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2780 if (mCalibration.havePressureScale) {
2781 mPressureScale = mCalibration.pressureScale;
2782 } else if (mRawPointerAxes.pressure.valid
2783 && mRawPointerAxes.pressure.maxValue != 0) {
2784 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002785 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002786 }
Jeff Brown8d608662010-08-30 03:02:23 -07002787
Jeff Brown65fd2512011-08-18 11:20:58 -07002788 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2789 mOrientedRanges.pressure.source = mSource;
2790 mOrientedRanges.pressure.min = 0;
2791 mOrientedRanges.pressure.max = 1.0;
2792 mOrientedRanges.pressure.flat = 0;
2793 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002794
Jeff Brown65fd2512011-08-18 11:20:58 -07002795 // Tilt
2796 mTiltXCenter = 0;
2797 mTiltXScale = 0;
2798 mTiltYCenter = 0;
2799 mTiltYScale = 0;
2800 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2801 if (mHaveTilt) {
2802 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2803 mRawPointerAxes.tiltX.maxValue);
2804 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2805 mRawPointerAxes.tiltY.maxValue);
2806 mTiltXScale = M_PI / 180;
2807 mTiltYScale = M_PI / 180;
2808
2809 mOrientedRanges.haveTilt = true;
2810
2811 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2812 mOrientedRanges.tilt.source = mSource;
2813 mOrientedRanges.tilt.min = 0;
2814 mOrientedRanges.tilt.max = M_PI_2;
2815 mOrientedRanges.tilt.flat = 0;
2816 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002817 }
2818
Jeff Brown8d608662010-08-30 03:02:23 -07002819 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002820 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002821 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002822 if (mHaveTilt) {
2823 mOrientedRanges.haveOrientation = true;
2824
2825 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2826 mOrientedRanges.orientation.source = mSource;
2827 mOrientedRanges.orientation.min = -M_PI;
2828 mOrientedRanges.orientation.max = M_PI;
2829 mOrientedRanges.orientation.flat = 0;
2830 mOrientedRanges.orientation.fuzz = 0;
2831 } else if (mCalibration.orientationCalibration !=
2832 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002833 if (mCalibration.orientationCalibration
2834 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002835 if (mRawPointerAxes.orientation.valid) {
2836 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2837 mRawPointerAxes.orientation.maxValue);
2838 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2839 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002840 }
2841 }
2842
Jeff Brownbe1aa822011-07-27 16:04:54 -07002843 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002844
Jeff Brownbe1aa822011-07-27 16:04:54 -07002845 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002846 mOrientedRanges.orientation.source = mSource;
2847 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002848 mOrientedRanges.orientation.max = M_PI_2;
2849 mOrientedRanges.orientation.flat = 0;
2850 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002851 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002852
2853 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002854 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002855 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2856 if (mCalibration.distanceCalibration
2857 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2858 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002859 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002860 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002861 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002862 }
2863 }
2864
Jeff Brownbe1aa822011-07-27 16:04:54 -07002865 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002866
Jeff Brownbe1aa822011-07-27 16:04:54 -07002867 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002868 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002869 mOrientedRanges.distance.min =
2870 mRawPointerAxes.distance.minValue * mDistanceScale;
2871 mOrientedRanges.distance.max =
2872 mRawPointerAxes.distance.minValue * mDistanceScale;
2873 mOrientedRanges.distance.flat = 0;
2874 mOrientedRanges.distance.fuzz =
2875 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002876 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002877 }
2878
Jeff Brown65fd2512011-08-18 11:20:58 -07002879 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002880 // Compute oriented surface dimensions, precision, scales and ranges.
2881 // Note that the maximum value reported is an inclusive maximum value so it is one
2882 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002883 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002884 case DISPLAY_ORIENTATION_90:
2885 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002886 mOrientedSurfaceWidth = mSurfaceHeight;
2887 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002888
Jeff Brownbe1aa822011-07-27 16:04:54 -07002889 mOrientedXPrecision = mYPrecision;
2890 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002891
Jeff Brownbe1aa822011-07-27 16:04:54 -07002892 mOrientedRanges.x.min = 0;
2893 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2894 * mYScale;
2895 mOrientedRanges.x.flat = 0;
2896 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002897
Jeff Brownbe1aa822011-07-27 16:04:54 -07002898 mOrientedRanges.y.min = 0;
2899 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2900 * mXScale;
2901 mOrientedRanges.y.flat = 0;
2902 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002903 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002904
Jeff Brown6d0fec22010-07-23 21:28:06 -07002905 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002906 mOrientedSurfaceWidth = mSurfaceWidth;
2907 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002908
Jeff Brownbe1aa822011-07-27 16:04:54 -07002909 mOrientedXPrecision = mXPrecision;
2910 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002911
Jeff Brownbe1aa822011-07-27 16:04:54 -07002912 mOrientedRanges.x.min = 0;
2913 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2914 * mXScale;
2915 mOrientedRanges.x.flat = 0;
2916 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002917
Jeff Brownbe1aa822011-07-27 16:04:54 -07002918 mOrientedRanges.y.min = 0;
2919 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2920 * mYScale;
2921 mOrientedRanges.y.flat = 0;
2922 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002923 break;
2924 }
Jeff Brownace13b12011-03-09 17:39:48 -08002925
2926 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002927 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002928 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2929 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002930 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002931 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2932 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002933
Jeff Brown2352b972011-04-12 22:39:53 -07002934 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002935 // given area relative to the diagonal size of the display when no acceleration
2936 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002937 // Assume that the touch pad has a square aspect ratio such that movements in
2938 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07002939 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002940 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002941 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002942
2943 // Scale zooms to cover a smaller range of the display than movements do.
2944 // This value determines the area around the pointer that is affected by freeform
2945 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07002946 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002947 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002948 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002949
Jeff Brown2352b972011-04-12 22:39:53 -07002950 // Max width between pointers to detect a swipe gesture is more than some fraction
2951 // of the diagonal axis of the touch pad. Touches that are wider than this are
2952 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002953 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002954 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002955 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002956
Jeff Brown65fd2512011-08-18 11:20:58 -07002957 // Abort current pointer usages because the state has changed.
2958 abortPointerUsage(when, 0 /*policyFlags*/);
2959
2960 // Inform the dispatcher about the changes.
2961 *outResetNeeded = true;
2962 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002963}
2964
Jeff Brownbe1aa822011-07-27 16:04:54 -07002965void TouchInputMapper::dumpSurface(String8& dump) {
2966 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2967 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2968 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002969}
2970
Jeff Brownbe1aa822011-07-27 16:04:54 -07002971void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002972 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002973 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002974
Jeff Brownbe1aa822011-07-27 16:04:54 -07002975 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002976
Jeff Brown6328cdc2010-07-29 18:18:33 -07002977 if (virtualKeyDefinitions.size() == 0) {
2978 return;
2979 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002980
Jeff Brownbe1aa822011-07-27 16:04:54 -07002981 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002982
Jeff Brownbe1aa822011-07-27 16:04:54 -07002983 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2984 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2985 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2986 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002987
2988 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002989 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002990 virtualKeyDefinitions[i];
2991
Jeff Brownbe1aa822011-07-27 16:04:54 -07002992 mVirtualKeys.add();
2993 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002994
2995 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2996 int32_t keyCode;
2997 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002998 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002999 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003000 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
3001 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003002 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003003 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003004 }
3005
Jeff Brown6328cdc2010-07-29 18:18:33 -07003006 virtualKey.keyCode = keyCode;
3007 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003008
Jeff Brown6328cdc2010-07-29 18:18:33 -07003009 // convert the key definition's display coordinates into touch coordinates for a hit box
3010 int32_t halfWidth = virtualKeyDefinition.width / 2;
3011 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003012
Jeff Brown6328cdc2010-07-29 18:18:33 -07003013 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003014 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003015 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003016 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003017 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003018 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003019 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003020 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003021 }
3022}
3023
Jeff Brownbe1aa822011-07-27 16:04:54 -07003024void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3025 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003026 dump.append(INDENT3 "Virtual Keys:\n");
3027
Jeff Brownbe1aa822011-07-27 16:04:54 -07003028 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3029 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003030 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3031 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3032 i, virtualKey.scanCode, virtualKey.keyCode,
3033 virtualKey.hitLeft, virtualKey.hitRight,
3034 virtualKey.hitTop, virtualKey.hitBottom);
3035 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003036 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003037}
3038
Jeff Brown8d608662010-08-30 03:02:23 -07003039void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003040 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003041 Calibration& out = mCalibration;
3042
Jeff Browna1f89ce2011-08-11 00:05:01 -07003043 // Size
3044 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3045 String8 sizeCalibrationString;
3046 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3047 if (sizeCalibrationString == "none") {
3048 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3049 } else if (sizeCalibrationString == "geometric") {
3050 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3051 } else if (sizeCalibrationString == "diameter") {
3052 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3053 } else if (sizeCalibrationString == "area") {
3054 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3055 } else if (sizeCalibrationString != "default") {
3056 LOGW("Invalid value for touch.size.calibration: '%s'",
3057 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003058 }
3059 }
3060
Jeff Browna1f89ce2011-08-11 00:05:01 -07003061 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3062 out.sizeScale);
3063 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3064 out.sizeBias);
3065 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3066 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003067
3068 // Pressure
3069 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3070 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003071 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003072 if (pressureCalibrationString == "none") {
3073 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3074 } else if (pressureCalibrationString == "physical") {
3075 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3076 } else if (pressureCalibrationString == "amplitude") {
3077 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3078 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003079 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003080 pressureCalibrationString.string());
3081 }
3082 }
3083
Jeff Brown8d608662010-08-30 03:02:23 -07003084 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3085 out.pressureScale);
3086
Jeff Brown8d608662010-08-30 03:02:23 -07003087 // Orientation
3088 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3089 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003090 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003091 if (orientationCalibrationString == "none") {
3092 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3093 } else if (orientationCalibrationString == "interpolated") {
3094 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003095 } else if (orientationCalibrationString == "vector") {
3096 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003097 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003098 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003099 orientationCalibrationString.string());
3100 }
3101 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003102
3103 // Distance
3104 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3105 String8 distanceCalibrationString;
3106 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3107 if (distanceCalibrationString == "none") {
3108 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3109 } else if (distanceCalibrationString == "scaled") {
3110 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3111 } else if (distanceCalibrationString != "default") {
3112 LOGW("Invalid value for touch.distance.calibration: '%s'",
3113 distanceCalibrationString.string());
3114 }
3115 }
3116
3117 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3118 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003119}
3120
3121void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003122 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003123 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3124 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3125 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003126 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003127 } else {
3128 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3129 }
Jeff Brown8d608662010-08-30 03:02:23 -07003130
Jeff Browna1f89ce2011-08-11 00:05:01 -07003131 // Pressure
3132 if (mRawPointerAxes.pressure.valid) {
3133 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3134 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3135 }
3136 } else {
3137 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003138 }
3139
3140 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003141 if (mRawPointerAxes.orientation.valid) {
3142 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003143 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003144 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003145 } else {
3146 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003147 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003148
3149 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003150 if (mRawPointerAxes.distance.valid) {
3151 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003152 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003153 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003154 } else {
3155 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003156 }
Jeff Brown8d608662010-08-30 03:02:23 -07003157}
3158
Jeff Brownef3d7e82010-09-30 14:33:04 -07003159void TouchInputMapper::dumpCalibration(String8& dump) {
3160 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003161
Jeff Browna1f89ce2011-08-11 00:05:01 -07003162 // Size
3163 switch (mCalibration.sizeCalibration) {
3164 case Calibration::SIZE_CALIBRATION_NONE:
3165 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003166 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003167 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3168 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003169 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003170 case Calibration::SIZE_CALIBRATION_DIAMETER:
3171 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3172 break;
3173 case Calibration::SIZE_CALIBRATION_AREA:
3174 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003175 break;
3176 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003177 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003178 }
3179
Jeff Browna1f89ce2011-08-11 00:05:01 -07003180 if (mCalibration.haveSizeScale) {
3181 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3182 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003183 }
3184
Jeff Browna1f89ce2011-08-11 00:05:01 -07003185 if (mCalibration.haveSizeBias) {
3186 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3187 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003188 }
3189
Jeff Browna1f89ce2011-08-11 00:05:01 -07003190 if (mCalibration.haveSizeIsSummed) {
3191 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3192 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003193 }
3194
3195 // Pressure
3196 switch (mCalibration.pressureCalibration) {
3197 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003198 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003199 break;
3200 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003201 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003202 break;
3203 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003204 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003205 break;
3206 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003207 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003208 }
3209
Jeff Brown8d608662010-08-30 03:02:23 -07003210 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003211 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3212 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003213 }
3214
Jeff Brown8d608662010-08-30 03:02:23 -07003215 // Orientation
3216 switch (mCalibration.orientationCalibration) {
3217 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003218 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003219 break;
3220 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003221 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003222 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003223 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3224 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3225 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003226 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003227 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003228 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003229
3230 // Distance
3231 switch (mCalibration.distanceCalibration) {
3232 case Calibration::DISTANCE_CALIBRATION_NONE:
3233 dump.append(INDENT4 "touch.distance.calibration: none\n");
3234 break;
3235 case Calibration::DISTANCE_CALIBRATION_SCALED:
3236 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3237 break;
3238 default:
3239 LOG_ASSERT(false);
3240 }
3241
3242 if (mCalibration.haveDistanceScale) {
3243 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3244 mCalibration.distanceScale);
3245 }
Jeff Brown8d608662010-08-30 03:02:23 -07003246}
3247
Jeff Brown65fd2512011-08-18 11:20:58 -07003248void TouchInputMapper::reset(nsecs_t when) {
3249 mCursorButtonAccumulator.reset(getDevice());
3250 mCursorScrollAccumulator.reset(getDevice());
3251 mTouchButtonAccumulator.reset(getDevice());
3252
3253 mPointerVelocityControl.reset();
3254 mWheelXVelocityControl.reset();
3255 mWheelYVelocityControl.reset();
3256
Jeff Brownbe1aa822011-07-27 16:04:54 -07003257 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003258 mLastRawPointerData.clear();
3259 mCurrentCookedPointerData.clear();
3260 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003261 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003262 mLastButtonState = 0;
3263 mCurrentRawVScroll = 0;
3264 mCurrentRawHScroll = 0;
3265 mCurrentFingerIdBits.clear();
3266 mLastFingerIdBits.clear();
3267 mCurrentStylusIdBits.clear();
3268 mLastStylusIdBits.clear();
3269 mCurrentMouseIdBits.clear();
3270 mLastMouseIdBits.clear();
3271 mPointerUsage = POINTER_USAGE_NONE;
3272 mSentHoverEnter = false;
3273 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003274
Jeff Brown65fd2512011-08-18 11:20:58 -07003275 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003276
Jeff Brown65fd2512011-08-18 11:20:58 -07003277 mPointerGesture.reset();
3278 mPointerSimple.reset();
3279
3280 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003281 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3282 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003283 }
3284
Jeff Brown65fd2512011-08-18 11:20:58 -07003285 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003286}
3287
Jeff Brown65fd2512011-08-18 11:20:58 -07003288void TouchInputMapper::process(const RawEvent* rawEvent) {
3289 mCursorButtonAccumulator.process(rawEvent);
3290 mCursorScrollAccumulator.process(rawEvent);
3291 mTouchButtonAccumulator.process(rawEvent);
3292
3293 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
3294 sync(rawEvent->when);
3295 }
3296}
3297
3298void TouchInputMapper::sync(nsecs_t when) {
3299 // Sync button state.
3300 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3301 | mCursorButtonAccumulator.getButtonState();
3302
3303 // Sync scroll state.
3304 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3305 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3306 mCursorScrollAccumulator.finishSync();
3307
3308 // Sync touch state.
3309 bool havePointerIds = true;
3310 mCurrentRawPointerData.clear();
3311 syncTouch(when, &havePointerIds);
3312
Jeff Brownaa3855d2011-03-17 01:34:19 -07003313#if DEBUG_RAW_EVENTS
3314 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003315 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3316 mLastRawPointerData.pointerCount,
3317 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003318 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003319 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3320 "hovering ids 0x%08x -> 0x%08x",
3321 mLastRawPointerData.pointerCount,
3322 mCurrentRawPointerData.pointerCount,
3323 mLastRawPointerData.touchingIdBits.value,
3324 mCurrentRawPointerData.touchingIdBits.value,
3325 mLastRawPointerData.hoveringIdBits.value,
3326 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003327 }
3328#endif
3329
Jeff Brown65fd2512011-08-18 11:20:58 -07003330 // Reset state that we will compute below.
3331 mCurrentFingerIdBits.clear();
3332 mCurrentStylusIdBits.clear();
3333 mCurrentMouseIdBits.clear();
3334 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003335
Jeff Brown65fd2512011-08-18 11:20:58 -07003336 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3337 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003338 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003339 mCurrentButtonState = 0;
3340 } else {
3341 // Preprocess pointer data.
3342 if (!havePointerIds) {
3343 assignPointerIds();
3344 }
3345
3346 // Handle policy on initial down or hover events.
3347 uint32_t policyFlags = 0;
3348 if (mLastRawPointerData.pointerCount == 0 && mCurrentRawPointerData.pointerCount != 0) {
3349 if (mDeviceMode == DEVICE_MODE_DIRECT) {
3350 // If this is a touch screen, hide the pointer on an initial down.
3351 getContext()->fadePointer();
3352 }
3353
3354 // Initial downs on external touch devices should wake the device.
3355 // We don't do this for internal touch screens to prevent them from waking
3356 // up in your pocket.
3357 // TODO: Use the input device configuration to control this behavior more finely.
3358 if (getDevice()->isExternal()) {
3359 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3360 }
3361 }
3362
3363 // Synthesize key down from raw buttons if needed.
3364 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3365 policyFlags, mLastButtonState, mCurrentButtonState);
3366
3367 // Consume raw off-screen touches before cooking pointer data.
3368 // If touches are consumed, subsequent code will not receive any pointer data.
3369 if (consumeRawTouches(when, policyFlags)) {
3370 mCurrentRawPointerData.clear();
3371 }
3372
3373 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3374 // with cooked pointer data that has the same ids and indices as the raw data.
3375 // The following code can use either the raw or cooked data, as needed.
3376 cookPointerData();
3377
3378 // Dispatch the touches either directly or by translation through a pointer on screen.
3379 if (mPointerController != NULL) {
3380 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3381 uint32_t id = idBits.clearFirstMarkedBit();
3382 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3383 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3384 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3385 mCurrentStylusIdBits.markBit(id);
3386 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3387 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3388 mCurrentFingerIdBits.markBit(id);
3389 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3390 mCurrentMouseIdBits.markBit(id);
3391 }
3392 }
3393 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3394 uint32_t id = idBits.clearFirstMarkedBit();
3395 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3396 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3397 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3398 mCurrentStylusIdBits.markBit(id);
3399 }
3400 }
3401
3402 // Stylus takes precedence over all tools, then mouse, then finger.
3403 PointerUsage pointerUsage = mPointerUsage;
3404 if (!mCurrentStylusIdBits.isEmpty()) {
3405 mCurrentMouseIdBits.clear();
3406 mCurrentFingerIdBits.clear();
3407 pointerUsage = POINTER_USAGE_STYLUS;
3408 } else if (!mCurrentMouseIdBits.isEmpty()) {
3409 mCurrentFingerIdBits.clear();
3410 pointerUsage = POINTER_USAGE_MOUSE;
3411 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3412 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003413 }
3414
3415 dispatchPointerUsage(when, policyFlags, pointerUsage);
3416 } else {
3417 dispatchHoverExit(when, policyFlags);
3418 dispatchTouches(when, policyFlags);
3419 dispatchHoverEnterAndMove(when, policyFlags);
3420 }
3421
3422 // Synthesize key up from raw buttons if needed.
3423 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3424 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003425 }
3426
Jeff Brown6328cdc2010-07-29 18:18:33 -07003427 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003428 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3429 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3430 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003431 mLastFingerIdBits = mCurrentFingerIdBits;
3432 mLastStylusIdBits = mCurrentStylusIdBits;
3433 mLastMouseIdBits = mCurrentMouseIdBits;
3434
3435 // Clear some transient state.
3436 mCurrentRawVScroll = 0;
3437 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003438}
3439
Jeff Brown79ac9692011-04-19 21:20:10 -07003440void TouchInputMapper::timeoutExpired(nsecs_t when) {
3441 if (mPointerController != NULL) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003442 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3443 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3444 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003445 }
3446}
3447
Jeff Brownbe1aa822011-07-27 16:04:54 -07003448bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3449 // Check for release of a virtual key.
3450 if (mCurrentVirtualKey.down) {
3451 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3452 // Pointer went up while virtual key was down.
3453 mCurrentVirtualKey.down = false;
3454 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003455#if DEBUG_VIRTUAL_KEYS
3456 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003457 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003458#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003459 dispatchVirtualKey(when, policyFlags,
3460 AKEY_EVENT_ACTION_UP,
3461 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003462 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003463 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003464 }
3465
Jeff Brownbe1aa822011-07-27 16:04:54 -07003466 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3467 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3468 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3469 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3470 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3471 // Pointer is still within the space of the virtual key.
3472 return true;
3473 }
3474 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003475
Jeff Brownbe1aa822011-07-27 16:04:54 -07003476 // Pointer left virtual key area or another pointer also went down.
3477 // Send key cancellation but do not consume the touch yet.
3478 // This is useful when the user swipes through from the virtual key area
3479 // into the main display surface.
3480 mCurrentVirtualKey.down = false;
3481 if (!mCurrentVirtualKey.ignored) {
3482#if DEBUG_VIRTUAL_KEYS
3483 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3484 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3485#endif
3486 dispatchVirtualKey(when, policyFlags,
3487 AKEY_EVENT_ACTION_UP,
3488 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3489 | AKEY_EVENT_FLAG_CANCELED);
3490 }
3491 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003492
Jeff Brownbe1aa822011-07-27 16:04:54 -07003493 if (mLastRawPointerData.touchingIdBits.isEmpty()
3494 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3495 // Pointer just went down. Check for virtual key press or off-screen touches.
3496 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3497 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3498 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3499 // If exactly one pointer went down, check for virtual key hit.
3500 // Otherwise we will drop the entire stroke.
3501 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3502 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3503 if (virtualKey) {
3504 mCurrentVirtualKey.down = true;
3505 mCurrentVirtualKey.downTime = when;
3506 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3507 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3508 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3509 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3510
3511 if (!mCurrentVirtualKey.ignored) {
3512#if DEBUG_VIRTUAL_KEYS
3513 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3514 mCurrentVirtualKey.keyCode,
3515 mCurrentVirtualKey.scanCode);
3516#endif
3517 dispatchVirtualKey(when, policyFlags,
3518 AKEY_EVENT_ACTION_DOWN,
3519 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3520 }
3521 }
3522 }
3523 return true;
3524 }
3525 }
3526
Jeff Brownfe508922011-01-18 15:10:10 -08003527 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003528 // most recent touch within the screen area. The idea is to filter out stray
3529 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003530 //
3531 // Problems we're trying to solve:
3532 //
3533 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3534 // virtual key area that is implemented by a separate touch panel and accidentally
3535 // triggers a virtual key.
3536 //
3537 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3538 // area and accidentally triggers a virtual key. This often happens when virtual keys
3539 // are layed out below the screen near to where the on screen keyboard's space bar
3540 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003541 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003542 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003543 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003544 return false;
3545}
3546
3547void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3548 int32_t keyEventAction, int32_t keyEventFlags) {
3549 int32_t keyCode = mCurrentVirtualKey.keyCode;
3550 int32_t scanCode = mCurrentVirtualKey.scanCode;
3551 nsecs_t downTime = mCurrentVirtualKey.downTime;
3552 int32_t metaState = mContext->getGlobalMetaState();
3553 policyFlags |= POLICY_FLAG_VIRTUAL;
3554
3555 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3556 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3557 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003558}
3559
Jeff Brown6d0fec22010-07-23 21:28:06 -07003560void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003561 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3562 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003563 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003564 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003565
3566 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003567 if (!currentIdBits.isEmpty()) {
3568 // No pointer id changes so this is a move event.
3569 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003570 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003571 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3572 AMOTION_EVENT_EDGE_FLAG_NONE,
3573 mCurrentCookedPointerData.pointerProperties,
3574 mCurrentCookedPointerData.pointerCoords,
3575 mCurrentCookedPointerData.idToIndex,
3576 currentIdBits, -1,
3577 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3578 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003579 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003580 // There may be pointers going up and pointers going down and pointers moving
3581 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003582 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3583 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003584 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003585 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003586
Jeff Brownace13b12011-03-09 17:39:48 -08003587 // Update last coordinates of pointers that have moved so that we observe the new
3588 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003589 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003590 mCurrentCookedPointerData.pointerProperties,
3591 mCurrentCookedPointerData.pointerCoords,
3592 mCurrentCookedPointerData.idToIndex,
3593 mLastCookedPointerData.pointerProperties,
3594 mLastCookedPointerData.pointerCoords,
3595 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003596 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003597 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003598 moveNeeded = true;
3599 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003600
Jeff Brownace13b12011-03-09 17:39:48 -08003601 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003602 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003603 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003604
Jeff Brown65fd2512011-08-18 11:20:58 -07003605 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003606 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003607 mLastCookedPointerData.pointerProperties,
3608 mLastCookedPointerData.pointerCoords,
3609 mLastCookedPointerData.idToIndex,
3610 dispatchedIdBits, upId,
3611 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003612 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003613 }
3614
Jeff Brownc3db8582010-10-20 15:33:38 -07003615 // Dispatch move events if any of the remaining pointers moved from their old locations.
3616 // Although applications receive new locations as part of individual pointer up
3617 // events, they do not generally handle them except when presented in a move event.
3618 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003619 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003620 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003621 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003622 mCurrentCookedPointerData.pointerProperties,
3623 mCurrentCookedPointerData.pointerCoords,
3624 mCurrentCookedPointerData.idToIndex,
3625 dispatchedIdBits, -1,
3626 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003627 }
3628
3629 // Dispatch pointer down events using the new pointer locations.
3630 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003631 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003632 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003633
Jeff Brownace13b12011-03-09 17:39:48 -08003634 if (dispatchedIdBits.count() == 1) {
3635 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003636 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003637 }
3638
Jeff Brown65fd2512011-08-18 11:20:58 -07003639 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003640 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003641 mCurrentCookedPointerData.pointerProperties,
3642 mCurrentCookedPointerData.pointerCoords,
3643 mCurrentCookedPointerData.idToIndex,
3644 dispatchedIdBits, downId,
3645 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003646 }
3647 }
Jeff Brownace13b12011-03-09 17:39:48 -08003648}
3649
Jeff Brownbe1aa822011-07-27 16:04:54 -07003650void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3651 if (mSentHoverEnter &&
3652 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3653 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3654 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003655 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003656 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3657 mLastCookedPointerData.pointerProperties,
3658 mLastCookedPointerData.pointerCoords,
3659 mLastCookedPointerData.idToIndex,
3660 mLastCookedPointerData.hoveringIdBits, -1,
3661 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3662 mSentHoverEnter = false;
3663 }
3664}
Jeff Brownace13b12011-03-09 17:39:48 -08003665
Jeff Brownbe1aa822011-07-27 16:04:54 -07003666void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3667 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3668 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3669 int32_t metaState = getContext()->getGlobalMetaState();
3670 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003671 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003672 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3673 mCurrentCookedPointerData.pointerProperties,
3674 mCurrentCookedPointerData.pointerCoords,
3675 mCurrentCookedPointerData.idToIndex,
3676 mCurrentCookedPointerData.hoveringIdBits, -1,
3677 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3678 mSentHoverEnter = true;
3679 }
Jeff Brownace13b12011-03-09 17:39:48 -08003680
Jeff Brown65fd2512011-08-18 11:20:58 -07003681 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003682 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3683 mCurrentCookedPointerData.pointerProperties,
3684 mCurrentCookedPointerData.pointerCoords,
3685 mCurrentCookedPointerData.idToIndex,
3686 mCurrentCookedPointerData.hoveringIdBits, -1,
3687 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3688 }
3689}
3690
3691void TouchInputMapper::cookPointerData() {
3692 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3693
3694 mCurrentCookedPointerData.clear();
3695 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3696 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3697 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3698
3699 // Walk through the the active pointers and map device coordinates onto
3700 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003701 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003702 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003703
Jeff Browna1f89ce2011-08-11 00:05:01 -07003704 // Size
3705 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3706 switch (mCalibration.sizeCalibration) {
3707 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3708 case Calibration::SIZE_CALIBRATION_DIAMETER:
3709 case Calibration::SIZE_CALIBRATION_AREA:
3710 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3711 touchMajor = in.touchMajor;
3712 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3713 toolMajor = in.toolMajor;
3714 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3715 size = mRawPointerAxes.touchMinor.valid
3716 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3717 } else if (mRawPointerAxes.touchMajor.valid) {
3718 toolMajor = touchMajor = in.touchMajor;
3719 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3720 ? in.touchMinor : in.touchMajor;
3721 size = mRawPointerAxes.touchMinor.valid
3722 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3723 } else if (mRawPointerAxes.toolMajor.valid) {
3724 touchMajor = toolMajor = in.toolMajor;
3725 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3726 ? in.toolMinor : in.toolMajor;
3727 size = mRawPointerAxes.toolMinor.valid
3728 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003729 } else {
Jeff Browna1f89ce2011-08-11 00:05:01 -07003730 LOG_ASSERT(false, "No touch or tool axes. "
3731 "Size calibration should have been resolved to NONE.");
3732 touchMajor = 0;
3733 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003734 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003735 toolMinor = 0;
3736 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003737 }
Jeff Brownace13b12011-03-09 17:39:48 -08003738
Jeff Browna1f89ce2011-08-11 00:05:01 -07003739 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3740 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3741 if (touchingCount > 1) {
3742 touchMajor /= touchingCount;
3743 touchMinor /= touchingCount;
3744 toolMajor /= touchingCount;
3745 toolMinor /= touchingCount;
3746 size /= touchingCount;
3747 }
3748 }
Jeff Brownace13b12011-03-09 17:39:48 -08003749
Jeff Browna1f89ce2011-08-11 00:05:01 -07003750 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3751 touchMajor *= mGeometricScale;
3752 touchMinor *= mGeometricScale;
3753 toolMajor *= mGeometricScale;
3754 toolMinor *= mGeometricScale;
3755 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3756 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003757 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003758 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3759 toolMinor = toolMajor;
3760 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3761 touchMinor = touchMajor;
3762 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003763 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003764
3765 mCalibration.applySizeScaleAndBias(&touchMajor);
3766 mCalibration.applySizeScaleAndBias(&touchMinor);
3767 mCalibration.applySizeScaleAndBias(&toolMajor);
3768 mCalibration.applySizeScaleAndBias(&toolMinor);
3769 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003770 break;
3771 default:
3772 touchMajor = 0;
3773 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003774 toolMajor = 0;
3775 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003776 size = 0;
3777 break;
3778 }
3779
Jeff Browna1f89ce2011-08-11 00:05:01 -07003780 // Pressure
3781 float pressure;
3782 switch (mCalibration.pressureCalibration) {
3783 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3784 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3785 pressure = in.pressure * mPressureScale;
3786 break;
3787 default:
3788 pressure = in.isHovering ? 0 : 1;
3789 break;
3790 }
3791
Jeff Brown65fd2512011-08-18 11:20:58 -07003792 // Tilt and Orientation
3793 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003794 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003795 if (mHaveTilt) {
3796 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3797 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3798 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3799 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3800 } else {
3801 tilt = 0;
3802
3803 switch (mCalibration.orientationCalibration) {
3804 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3805 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3806 break;
3807 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3808 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3809 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3810 if (c1 != 0 || c2 != 0) {
3811 orientation = atan2f(c1, c2) * 0.5f;
3812 float confidence = hypotf(c1, c2);
3813 float scale = 1.0f + confidence / 16.0f;
3814 touchMajor *= scale;
3815 touchMinor /= scale;
3816 toolMajor *= scale;
3817 toolMinor /= scale;
3818 } else {
3819 orientation = 0;
3820 }
3821 break;
3822 }
3823 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003824 orientation = 0;
3825 }
Jeff Brownace13b12011-03-09 17:39:48 -08003826 }
3827
Jeff Brown80fd47c2011-05-24 01:07:44 -07003828 // Distance
3829 float distance;
3830 switch (mCalibration.distanceCalibration) {
3831 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003832 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003833 break;
3834 default:
3835 distance = 0;
3836 }
3837
Jeff Brownace13b12011-03-09 17:39:48 -08003838 // X and Y
3839 // Adjust coords for surface orientation.
3840 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003841 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003842 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003843 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3844 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003845 orientation -= M_PI_2;
3846 if (orientation < - M_PI_2) {
3847 orientation += M_PI;
3848 }
3849 break;
3850 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003851 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3852 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003853 break;
3854 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003855 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3856 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003857 orientation += M_PI_2;
3858 if (orientation > M_PI_2) {
3859 orientation -= M_PI;
3860 }
3861 break;
3862 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003863 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3864 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003865 break;
3866 }
3867
3868 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003869 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003870 out.clear();
3871 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3872 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3873 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3874 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3875 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3876 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3877 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3878 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3879 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003880 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003881 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003882
3883 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003884 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3885 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003886 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003887 properties.id = id;
3888 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003889
Jeff Brownbe1aa822011-07-27 16:04:54 -07003890 // Write id index.
3891 mCurrentCookedPointerData.idToIndex[id] = i;
3892 }
Jeff Brownace13b12011-03-09 17:39:48 -08003893}
3894
Jeff Brown65fd2512011-08-18 11:20:58 -07003895void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3896 PointerUsage pointerUsage) {
3897 if (pointerUsage != mPointerUsage) {
3898 abortPointerUsage(when, policyFlags);
3899 mPointerUsage = pointerUsage;
3900 }
3901
3902 switch (mPointerUsage) {
3903 case POINTER_USAGE_GESTURES:
3904 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3905 break;
3906 case POINTER_USAGE_STYLUS:
3907 dispatchPointerStylus(when, policyFlags);
3908 break;
3909 case POINTER_USAGE_MOUSE:
3910 dispatchPointerMouse(when, policyFlags);
3911 break;
3912 default:
3913 break;
3914 }
3915}
3916
3917void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3918 switch (mPointerUsage) {
3919 case POINTER_USAGE_GESTURES:
3920 abortPointerGestures(when, policyFlags);
3921 break;
3922 case POINTER_USAGE_STYLUS:
3923 abortPointerStylus(when, policyFlags);
3924 break;
3925 case POINTER_USAGE_MOUSE:
3926 abortPointerMouse(when, policyFlags);
3927 break;
3928 default:
3929 break;
3930 }
3931
3932 mPointerUsage = POINTER_USAGE_NONE;
3933}
3934
Jeff Brown79ac9692011-04-19 21:20:10 -07003935void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3936 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003937 // Update current gesture coordinates.
3938 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003939 bool sendEvents = preparePointerGestures(when,
3940 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3941 if (!sendEvents) {
3942 return;
3943 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003944 if (finishPreviousGesture) {
3945 cancelPreviousGesture = false;
3946 }
Jeff Brownace13b12011-03-09 17:39:48 -08003947
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003948 // Update the pointer presentation and spots.
3949 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3950 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3951 if (finishPreviousGesture || cancelPreviousGesture) {
3952 mPointerController->clearSpots();
3953 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003954 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3955 mPointerGesture.currentGestureIdToIndex,
3956 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003957 } else {
3958 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3959 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003960
Jeff Brown538881e2011-05-25 18:23:38 -07003961 // Show or hide the pointer if needed.
3962 switch (mPointerGesture.currentGestureMode) {
3963 case PointerGesture::NEUTRAL:
3964 case PointerGesture::QUIET:
3965 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3966 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3967 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3968 // Remind the user of where the pointer is after finishing a gesture with spots.
3969 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3970 }
3971 break;
3972 case PointerGesture::TAP:
3973 case PointerGesture::TAP_DRAG:
3974 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3975 case PointerGesture::HOVER:
3976 case PointerGesture::PRESS:
3977 // Unfade the pointer when the current gesture manipulates the
3978 // area directly under the pointer.
3979 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3980 break;
3981 case PointerGesture::SWIPE:
3982 case PointerGesture::FREEFORM:
3983 // Fade the pointer when the current gesture manipulates a different
3984 // area and there are spots to guide the user experience.
3985 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3986 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3987 } else {
3988 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3989 }
3990 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003991 }
3992
Jeff Brownace13b12011-03-09 17:39:48 -08003993 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003994 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003995 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003996
3997 // Update last coordinates of pointers that have moved so that we observe the new
3998 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003999 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4000 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4001 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004002 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004003 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4004 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4005 bool moveNeeded = false;
4006 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004007 && !mPointerGesture.lastGestureIdBits.isEmpty()
4008 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004009 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4010 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004011 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004012 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004013 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004014 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4015 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004016 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004017 moveNeeded = true;
4018 }
Jeff Brownace13b12011-03-09 17:39:48 -08004019 }
4020
4021 // Send motion events for all pointers that went up or were canceled.
4022 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4023 if (!dispatchedGestureIdBits.isEmpty()) {
4024 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004025 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004026 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4027 AMOTION_EVENT_EDGE_FLAG_NONE,
4028 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004029 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4030 dispatchedGestureIdBits, -1,
4031 0, 0, mPointerGesture.downTime);
4032
4033 dispatchedGestureIdBits.clear();
4034 } else {
4035 BitSet32 upGestureIdBits;
4036 if (finishPreviousGesture) {
4037 upGestureIdBits = dispatchedGestureIdBits;
4038 } else {
4039 upGestureIdBits.value = dispatchedGestureIdBits.value
4040 & ~mPointerGesture.currentGestureIdBits.value;
4041 }
4042 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004043 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004044
Jeff Brown65fd2512011-08-18 11:20:58 -07004045 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004046 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004047 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4048 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004049 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4050 dispatchedGestureIdBits, id,
4051 0, 0, mPointerGesture.downTime);
4052
4053 dispatchedGestureIdBits.clearBit(id);
4054 }
4055 }
4056 }
4057
4058 // Send motion events for all pointers that moved.
4059 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004060 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004061 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4062 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004063 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4064 dispatchedGestureIdBits, -1,
4065 0, 0, mPointerGesture.downTime);
4066 }
4067
4068 // Send motion events for all pointers that went down.
4069 if (down) {
4070 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4071 & ~dispatchedGestureIdBits.value);
4072 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004073 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004074 dispatchedGestureIdBits.markBit(id);
4075
Jeff Brownace13b12011-03-09 17:39:48 -08004076 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004077 mPointerGesture.downTime = when;
4078 }
4079
Jeff Brown65fd2512011-08-18 11:20:58 -07004080 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004081 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004082 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004083 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4084 dispatchedGestureIdBits, id,
4085 0, 0, mPointerGesture.downTime);
4086 }
4087 }
4088
Jeff Brownace13b12011-03-09 17:39:48 -08004089 // Send motion events for hover.
4090 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004091 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004092 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4093 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4094 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004095 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4096 mPointerGesture.currentGestureIdBits, -1,
4097 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004098 } else if (dispatchedGestureIdBits.isEmpty()
4099 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4100 // Synthesize a hover move event after all pointers go up to indicate that
4101 // the pointer is hovering again even if the user is not currently touching
4102 // the touch pad. This ensures that a view will receive a fresh hover enter
4103 // event after a tap.
4104 float x, y;
4105 mPointerController->getPosition(&x, &y);
4106
4107 PointerProperties pointerProperties;
4108 pointerProperties.clear();
4109 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004110 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004111
4112 PointerCoords pointerCoords;
4113 pointerCoords.clear();
4114 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4115 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4116
Jeff Brown65fd2512011-08-18 11:20:58 -07004117 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004118 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4119 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4120 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004121 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004122 }
4123
4124 // Update state.
4125 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4126 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004127 mPointerGesture.lastGestureIdBits.clear();
4128 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004129 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4130 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004131 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004132 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004133 mPointerGesture.lastGestureProperties[index].copyFrom(
4134 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004135 mPointerGesture.lastGestureCoords[index].copyFrom(
4136 mPointerGesture.currentGestureCoords[index]);
4137 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004138 }
4139 }
4140}
4141
Jeff Brown65fd2512011-08-18 11:20:58 -07004142void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4143 // Cancel previously dispatches pointers.
4144 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4145 int32_t metaState = getContext()->getGlobalMetaState();
4146 int32_t buttonState = mCurrentButtonState;
4147 dispatchMotion(when, policyFlags, mSource,
4148 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4149 AMOTION_EVENT_EDGE_FLAG_NONE,
4150 mPointerGesture.lastGestureProperties,
4151 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4152 mPointerGesture.lastGestureIdBits, -1,
4153 0, 0, mPointerGesture.downTime);
4154 }
4155
4156 // Reset the current pointer gesture.
4157 mPointerGesture.reset();
4158 mPointerVelocityControl.reset();
4159
4160 // Remove any current spots.
4161 if (mPointerController != NULL) {
4162 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4163 mPointerController->clearSpots();
4164 }
4165}
4166
Jeff Brown79ac9692011-04-19 21:20:10 -07004167bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4168 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004169 *outCancelPreviousGesture = false;
4170 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004171
Jeff Brown79ac9692011-04-19 21:20:10 -07004172 // Handle TAP timeout.
4173 if (isTimeout) {
4174#if DEBUG_GESTURES
4175 LOGD("Gestures: Processing timeout");
4176#endif
4177
4178 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004179 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004180 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004181 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004182 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004183 } else {
4184 // The tap is finished.
4185#if DEBUG_GESTURES
4186 LOGD("Gestures: TAP finished");
4187#endif
4188 *outFinishPreviousGesture = true;
4189
4190 mPointerGesture.activeGestureId = -1;
4191 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4192 mPointerGesture.currentGestureIdBits.clear();
4193
Jeff Brown65fd2512011-08-18 11:20:58 -07004194 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004195 return true;
4196 }
4197 }
4198
4199 // We did not handle this timeout.
4200 return false;
4201 }
4202
Jeff Brown65fd2512011-08-18 11:20:58 -07004203 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4204 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4205
Jeff Brownace13b12011-03-09 17:39:48 -08004206 // Update the velocity tracker.
4207 {
4208 VelocityTracker::Position positions[MAX_POINTERS];
4209 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004210 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004211 uint32_t id = idBits.clearFirstMarkedBit();
4212 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004213 positions[count].x = pointer.x * mPointerXMovementScale;
4214 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004215 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004216 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004217 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004218 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004219
Jeff Brownace13b12011-03-09 17:39:48 -08004220 // Pick a new active touch id if needed.
4221 // Choose an arbitrary pointer that just went down, if there is one.
4222 // Otherwise choose an arbitrary remaining pointer.
4223 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004224 // We keep the same active touch id for as long as possible.
4225 bool activeTouchChanged = false;
4226 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4227 int32_t activeTouchId = lastActiveTouchId;
4228 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004229 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004230 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004231 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004232 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004233 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004234 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004235 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004236 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004237 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004238 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004239 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004240 } else {
4241 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004242 }
4243 }
4244
4245 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004246 bool isQuietTime = false;
4247 if (activeTouchId < 0) {
4248 mPointerGesture.resetQuietTime();
4249 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004250 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004251 if (!isQuietTime) {
4252 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4253 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4254 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004255 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004256 // Enter quiet time when exiting swipe or freeform state.
4257 // This is to prevent accidentally entering the hover state and flinging the
4258 // pointer when finishing a swipe and there is still one pointer left onscreen.
4259 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004260 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004261 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004262 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004263 // Enter quiet time when releasing the button and there are still two or more
4264 // fingers down. This may indicate that one finger was used to press the button
4265 // but it has not gone up yet.
4266 isQuietTime = true;
4267 }
4268 if (isQuietTime) {
4269 mPointerGesture.quietTime = when;
4270 }
Jeff Brownace13b12011-03-09 17:39:48 -08004271 }
4272 }
4273
4274 // Switch states based on button and pointer state.
4275 if (isQuietTime) {
4276 // Case 1: Quiet time. (QUIET)
4277#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004278 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004279 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004280#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004281 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4282 *outFinishPreviousGesture = true;
4283 }
Jeff Brownace13b12011-03-09 17:39:48 -08004284
4285 mPointerGesture.activeGestureId = -1;
4286 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004287 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004288
Jeff Brown65fd2512011-08-18 11:20:58 -07004289 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004290 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004291 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004292 // The pointer follows the active touch point.
4293 // Emit DOWN, MOVE, UP events at the pointer location.
4294 //
4295 // Only the active touch matters; other fingers are ignored. This policy helps
4296 // to handle the case where the user places a second finger on the touch pad
4297 // to apply the necessary force to depress an integrated button below the surface.
4298 // We don't want the second finger to be delivered to applications.
4299 //
4300 // For this to work well, we need to make sure to track the pointer that is really
4301 // active. If the user first puts one finger down to click then adds another
4302 // finger to drag then the active pointer should switch to the finger that is
4303 // being dragged.
4304#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004305 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004306 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004307#endif
4308 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004309 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004310 *outFinishPreviousGesture = true;
4311 mPointerGesture.activeGestureId = 0;
4312 }
4313
4314 // Switch pointers if needed.
4315 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004316 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004317 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004318 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004319 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004320 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004321 float vx, vy;
4322 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4323 float speed = hypotf(vx, vy);
4324 if (speed > bestSpeed) {
4325 bestId = id;
4326 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004327 }
Jeff Brown8d608662010-08-30 03:02:23 -07004328 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004329 }
4330 if (bestId >= 0 && bestId != activeTouchId) {
4331 mPointerGesture.activeTouchId = activeTouchId = bestId;
4332 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004333#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07004334 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4335 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004336#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004337 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004338 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004339
Jeff Brown65fd2512011-08-18 11:20:58 -07004340 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004341 const RawPointerData::Pointer& currentPointer =
4342 mCurrentRawPointerData.pointerForId(activeTouchId);
4343 const RawPointerData::Pointer& lastPointer =
4344 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004345 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4346 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004347
Jeff Brownbe1aa822011-07-27 16:04:54 -07004348 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004349 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004350
4351 // Move the pointer using a relative motion.
4352 // When using spots, the click will occur at the position of the anchor
4353 // spot and all other spots will move there.
4354 mPointerController->move(deltaX, deltaY);
4355 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004356 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004357 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004358
Jeff Brownace13b12011-03-09 17:39:48 -08004359 float x, y;
4360 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004361
Jeff Brown79ac9692011-04-19 21:20:10 -07004362 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004363 mPointerGesture.currentGestureIdBits.clear();
4364 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4365 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004366 mPointerGesture.currentGestureProperties[0].clear();
4367 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004368 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004369 mPointerGesture.currentGestureCoords[0].clear();
4370 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4371 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4372 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004373 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004374 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004375 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4376 *outFinishPreviousGesture = true;
4377 }
Jeff Brownace13b12011-03-09 17:39:48 -08004378
Jeff Brown79ac9692011-04-19 21:20:10 -07004379 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004380 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004381 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004382 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4383 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004384 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004385 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004386 float x, y;
4387 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004388 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4389 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004390#if DEBUG_GESTURES
4391 LOGD("Gestures: TAP");
4392#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004393
4394 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004395 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004396 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004397
Jeff Brownace13b12011-03-09 17:39:48 -08004398 mPointerGesture.activeGestureId = 0;
4399 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004400 mPointerGesture.currentGestureIdBits.clear();
4401 mPointerGesture.currentGestureIdBits.markBit(
4402 mPointerGesture.activeGestureId);
4403 mPointerGesture.currentGestureIdToIndex[
4404 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004405 mPointerGesture.currentGestureProperties[0].clear();
4406 mPointerGesture.currentGestureProperties[0].id =
4407 mPointerGesture.activeGestureId;
4408 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004409 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004410 mPointerGesture.currentGestureCoords[0].clear();
4411 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004412 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004413 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004414 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004415 mPointerGesture.currentGestureCoords[0].setAxisValue(
4416 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004417
Jeff Brownace13b12011-03-09 17:39:48 -08004418 tapped = true;
4419 } else {
4420#if DEBUG_GESTURES
4421 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004422 x - mPointerGesture.tapX,
4423 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004424#endif
4425 }
4426 } else {
4427#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004428 LOGD("Gestures: Not a TAP, %0.3fms since down",
4429 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004430#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004431 }
Jeff Brownace13b12011-03-09 17:39:48 -08004432 }
Jeff Brown2352b972011-04-12 22:39:53 -07004433
Jeff Brown65fd2512011-08-18 11:20:58 -07004434 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004435
Jeff Brownace13b12011-03-09 17:39:48 -08004436 if (!tapped) {
4437#if DEBUG_GESTURES
4438 LOGD("Gestures: NEUTRAL");
4439#endif
4440 mPointerGesture.activeGestureId = -1;
4441 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004442 mPointerGesture.currentGestureIdBits.clear();
4443 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004444 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004445 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004446 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004447 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4448 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004449 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004450
Jeff Brown79ac9692011-04-19 21:20:10 -07004451 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4452 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004453 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004454 float x, y;
4455 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004456 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4457 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004458 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4459 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004460#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004461 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4462 x - mPointerGesture.tapX,
4463 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004464#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004465 }
4466 } else {
4467#if DEBUG_GESTURES
4468 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4469 (when - mPointerGesture.tapUpTime) * 0.000001f);
4470#endif
4471 }
4472 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4473 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4474 }
Jeff Brownace13b12011-03-09 17:39:48 -08004475
Jeff Brown65fd2512011-08-18 11:20:58 -07004476 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004477 const RawPointerData::Pointer& currentPointer =
4478 mCurrentRawPointerData.pointerForId(activeTouchId);
4479 const RawPointerData::Pointer& lastPointer =
4480 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004481 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004482 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004483 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004484 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004485
Jeff Brownbe1aa822011-07-27 16:04:54 -07004486 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004487 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004488
Jeff Brown2352b972011-04-12 22:39:53 -07004489 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004490 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004491 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004492 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004493 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004494 }
4495
Jeff Brown79ac9692011-04-19 21:20:10 -07004496 bool down;
4497 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4498#if DEBUG_GESTURES
4499 LOGD("Gestures: TAP_DRAG");
4500#endif
4501 down = true;
4502 } else {
4503#if DEBUG_GESTURES
4504 LOGD("Gestures: HOVER");
4505#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004506 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4507 *outFinishPreviousGesture = true;
4508 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004509 mPointerGesture.activeGestureId = 0;
4510 down = false;
4511 }
Jeff Brownace13b12011-03-09 17:39:48 -08004512
4513 float x, y;
4514 mPointerController->getPosition(&x, &y);
4515
Jeff Brownace13b12011-03-09 17:39:48 -08004516 mPointerGesture.currentGestureIdBits.clear();
4517 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4518 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004519 mPointerGesture.currentGestureProperties[0].clear();
4520 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4521 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004522 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004523 mPointerGesture.currentGestureCoords[0].clear();
4524 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4525 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004526 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4527 down ? 1.0f : 0.0f);
4528
Jeff Brown65fd2512011-08-18 11:20:58 -07004529 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004530 mPointerGesture.resetTap();
4531 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004532 mPointerGesture.tapX = x;
4533 mPointerGesture.tapY = y;
4534 }
Jeff Brownace13b12011-03-09 17:39:48 -08004535 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004536 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4537 // We need to provide feedback for each finger that goes down so we cannot wait
4538 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004539 //
Jeff Brown2352b972011-04-12 22:39:53 -07004540 // The ambiguous case is deciding what to do when there are two fingers down but they
4541 // have not moved enough to determine whether they are part of a drag or part of a
4542 // freeform gesture, or just a press or long-press at the pointer location.
4543 //
4544 // When there are two fingers we start with the PRESS hypothesis and we generate a
4545 // down at the pointer location.
4546 //
4547 // When the two fingers move enough or when additional fingers are added, we make
4548 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004549 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004550
Jeff Brown214eaf42011-05-26 19:17:02 -07004551 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004552 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004553 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004554 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4555 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004556 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004557 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004558 // Additional pointers have gone down but not yet settled.
4559 // Reset the gesture.
4560#if DEBUG_GESTURES
4561 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004562 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004563 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004564 * 0.000001f);
4565#endif
4566 *outCancelPreviousGesture = true;
4567 } else {
4568 // Continue previous gesture.
4569 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4570 }
4571
4572 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004573 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4574 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004575 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004576 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004577
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004578 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004579#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004580 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4581 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004582 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004583 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004584#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004585 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4586 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004587 &mPointerGesture.referenceTouchY);
4588 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4589 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004590 }
Jeff Brownace13b12011-03-09 17:39:48 -08004591
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004592 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004593 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004594 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4595 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004596 mPointerGesture.referenceDeltas[id].dx = 0;
4597 mPointerGesture.referenceDeltas[id].dy = 0;
4598 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004599 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004600
4601 // Add delta for all fingers and calculate a common movement delta.
4602 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004603 BitSet32 commonIdBits(mLastFingerIdBits.value
4604 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004605 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4606 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004607 uint32_t id = idBits.clearFirstMarkedBit();
4608 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4609 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004610 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4611 delta.dx += cpd.x - lpd.x;
4612 delta.dy += cpd.y - lpd.y;
4613
4614 if (first) {
4615 commonDeltaX = delta.dx;
4616 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004617 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004618 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4619 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4620 }
4621 }
Jeff Brownace13b12011-03-09 17:39:48 -08004622
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004623 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4624 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4625 float dist[MAX_POINTER_ID + 1];
4626 int32_t distOverThreshold = 0;
4627 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004628 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004629 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004630 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4631 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004632 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004633 distOverThreshold += 1;
4634 }
4635 }
4636
4637 // Only transition when at least two pointers have moved further than
4638 // the minimum distance threshold.
4639 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004640 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004641 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004642#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004643 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004644 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004645#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004646 *outCancelPreviousGesture = true;
4647 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4648 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004649 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004650 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004651 uint32_t id1 = idBits.clearFirstMarkedBit();
4652 uint32_t id2 = idBits.firstMarkedBit();
4653 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4654 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4655 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4656 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4657 // There are two pointers but they are too far apart for a SWIPE,
4658 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004659#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004660 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4661 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004662#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004663 *outCancelPreviousGesture = true;
4664 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4665 } else {
4666 // There are two pointers. Wait for both pointers to start moving
4667 // before deciding whether this is a SWIPE or FREEFORM gesture.
4668 float dist1 = dist[id1];
4669 float dist2 = dist[id2];
4670 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4671 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4672 // Calculate the dot product of the displacement vectors.
4673 // When the vectors are oriented in approximately the same direction,
4674 // the angle betweeen them is near zero and the cosine of the angle
4675 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4676 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4677 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004678 float dx1 = delta1.dx * mPointerXZoomScale;
4679 float dy1 = delta1.dy * mPointerYZoomScale;
4680 float dx2 = delta2.dx * mPointerXZoomScale;
4681 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004682 float dot = dx1 * dx2 + dy1 * dy2;
4683 float cosine = dot / (dist1 * dist2); // denominator always > 0
4684 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4685 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004686#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004687 LOGD("Gestures: PRESS transitioned to SWIPE, "
4688 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4689 "cosine %0.3f >= %0.3f",
4690 dist1, mConfig.pointerGestureMultitouchMinDistance,
4691 dist2, mConfig.pointerGestureMultitouchMinDistance,
4692 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004693#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004694 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4695 } else {
4696 // Pointers are moving in different directions. Switch to FREEFORM.
4697#if DEBUG_GESTURES
4698 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4699 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4700 "cosine %0.3f < %0.3f",
4701 dist1, mConfig.pointerGestureMultitouchMinDistance,
4702 dist2, mConfig.pointerGestureMultitouchMinDistance,
4703 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4704#endif
4705 *outCancelPreviousGesture = true;
4706 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4707 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004708 }
Jeff Brownace13b12011-03-09 17:39:48 -08004709 }
4710 }
Jeff Brownace13b12011-03-09 17:39:48 -08004711 }
4712 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004713 // Switch from SWIPE to FREEFORM if additional pointers go down.
4714 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004715 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004716#if DEBUG_GESTURES
4717 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004718 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004719#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004720 *outCancelPreviousGesture = true;
4721 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004722 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004723 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004724
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004725 // Move the reference points based on the overall group motion of the fingers
4726 // except in PRESS mode while waiting for a transition to occur.
4727 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4728 && (commonDeltaX || commonDeltaY)) {
4729 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004730 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004731 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004732 delta.dx = 0;
4733 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004734 }
4735
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004736 mPointerGesture.referenceTouchX += commonDeltaX;
4737 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004738
Jeff Brown65fd2512011-08-18 11:20:58 -07004739 commonDeltaX *= mPointerXMovementScale;
4740 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004741
Jeff Brownbe1aa822011-07-27 16:04:54 -07004742 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004743 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004744
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004745 mPointerGesture.referenceGestureX += commonDeltaX;
4746 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004747 }
4748
4749 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004750 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4751 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4752 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004753#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004754 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004755 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004756 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004757#endif
4758 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4759
4760 mPointerGesture.currentGestureIdBits.clear();
4761 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4762 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004763 mPointerGesture.currentGestureProperties[0].clear();
4764 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4765 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004766 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004767 mPointerGesture.currentGestureCoords[0].clear();
4768 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4769 mPointerGesture.referenceGestureX);
4770 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4771 mPointerGesture.referenceGestureY);
4772 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004773 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4774 // FREEFORM mode.
4775#if DEBUG_GESTURES
4776 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4777 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004778 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004779#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004780 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004781
Jeff Brownace13b12011-03-09 17:39:48 -08004782 mPointerGesture.currentGestureIdBits.clear();
4783
4784 BitSet32 mappedTouchIdBits;
4785 BitSet32 usedGestureIdBits;
4786 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4787 // Initially, assign the active gesture id to the active touch point
4788 // if there is one. No other touch id bits are mapped yet.
4789 if (!*outCancelPreviousGesture) {
4790 mappedTouchIdBits.markBit(activeTouchId);
4791 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4792 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4793 mPointerGesture.activeGestureId;
4794 } else {
4795 mPointerGesture.activeGestureId = -1;
4796 }
4797 } else {
4798 // Otherwise, assume we mapped all touches from the previous frame.
4799 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004800 mappedTouchIdBits.value = mLastFingerIdBits.value
4801 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004802 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4803
4804 // Check whether we need to choose a new active gesture id because the
4805 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004806 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4807 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004808 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004809 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004810 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4811 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4812 mPointerGesture.activeGestureId = -1;
4813 break;
4814 }
4815 }
4816 }
4817
4818#if DEBUG_GESTURES
4819 LOGD("Gestures: FREEFORM follow up "
4820 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4821 "activeGestureId=%d",
4822 mappedTouchIdBits.value, usedGestureIdBits.value,
4823 mPointerGesture.activeGestureId);
4824#endif
4825
Jeff Brown65fd2512011-08-18 11:20:58 -07004826 BitSet32 idBits(mCurrentFingerIdBits);
4827 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004828 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004829 uint32_t gestureId;
4830 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004831 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004832 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4833#if DEBUG_GESTURES
4834 LOGD("Gestures: FREEFORM "
4835 "new mapping for touch id %d -> gesture id %d",
4836 touchId, gestureId);
4837#endif
4838 } else {
4839 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4840#if DEBUG_GESTURES
4841 LOGD("Gestures: FREEFORM "
4842 "existing mapping for touch id %d -> gesture id %d",
4843 touchId, gestureId);
4844#endif
4845 }
4846 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4847 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4848
Jeff Brownbe1aa822011-07-27 16:04:54 -07004849 const RawPointerData::Pointer& pointer =
4850 mCurrentRawPointerData.pointerForId(touchId);
4851 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004852 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004853 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004854 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004855 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004856
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004857 mPointerGesture.currentGestureProperties[i].clear();
4858 mPointerGesture.currentGestureProperties[i].id = gestureId;
4859 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004860 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004861 mPointerGesture.currentGestureCoords[i].clear();
4862 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004863 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004864 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004865 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004866 mPointerGesture.currentGestureCoords[i].setAxisValue(
4867 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4868 }
4869
4870 if (mPointerGesture.activeGestureId < 0) {
4871 mPointerGesture.activeGestureId =
4872 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4873#if DEBUG_GESTURES
4874 LOGD("Gestures: FREEFORM new "
4875 "activeGestureId=%d", mPointerGesture.activeGestureId);
4876#endif
4877 }
Jeff Brown2352b972011-04-12 22:39:53 -07004878 }
Jeff Brownace13b12011-03-09 17:39:48 -08004879 }
4880
Jeff Brownbe1aa822011-07-27 16:04:54 -07004881 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004882
Jeff Brownace13b12011-03-09 17:39:48 -08004883#if DEBUG_GESTURES
4884 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004885 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4886 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004887 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004888 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4889 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004890 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004891 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004892 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004893 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004894 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004895 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4896 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4897 id, index, properties.toolType,
4898 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004899 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4900 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4901 }
4902 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004903 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004904 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004905 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004906 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004907 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4908 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4909 id, index, properties.toolType,
4910 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004911 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4912 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4913 }
4914#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004915 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004916}
4917
Jeff Brown65fd2512011-08-18 11:20:58 -07004918void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4919 mPointerSimple.currentCoords.clear();
4920 mPointerSimple.currentProperties.clear();
4921
4922 bool down, hovering;
4923 if (!mCurrentStylusIdBits.isEmpty()) {
4924 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4925 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4926 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4927 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4928 mPointerController->setPosition(x, y);
4929
4930 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4931 down = !hovering;
4932
4933 mPointerController->getPosition(&x, &y);
4934 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4935 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4936 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4937 mPointerSimple.currentProperties.id = 0;
4938 mPointerSimple.currentProperties.toolType =
4939 mCurrentCookedPointerData.pointerProperties[index].toolType;
4940 } else {
4941 down = false;
4942 hovering = false;
4943 }
4944
4945 dispatchPointerSimple(when, policyFlags, down, hovering);
4946}
4947
4948void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
4949 abortPointerSimple(when, policyFlags);
4950}
4951
4952void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
4953 mPointerSimple.currentCoords.clear();
4954 mPointerSimple.currentProperties.clear();
4955
4956 bool down, hovering;
4957 if (!mCurrentMouseIdBits.isEmpty()) {
4958 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
4959 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
4960 if (mLastMouseIdBits.hasBit(id)) {
4961 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
4962 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
4963 - mLastRawPointerData.pointers[lastIndex].x)
4964 * mPointerXMovementScale;
4965 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
4966 - mLastRawPointerData.pointers[lastIndex].y)
4967 * mPointerYMovementScale;
4968
4969 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
4970 mPointerVelocityControl.move(when, &deltaX, &deltaY);
4971
4972 mPointerController->move(deltaX, deltaY);
4973 } else {
4974 mPointerVelocityControl.reset();
4975 }
4976
4977 down = isPointerDown(mCurrentButtonState);
4978 hovering = !down;
4979
4980 float x, y;
4981 mPointerController->getPosition(&x, &y);
4982 mPointerSimple.currentCoords.copyFrom(
4983 mCurrentCookedPointerData.pointerCoords[currentIndex]);
4984 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4985 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4986 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4987 hovering ? 0.0f : 1.0f);
4988 mPointerSimple.currentProperties.id = 0;
4989 mPointerSimple.currentProperties.toolType =
4990 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
4991 } else {
4992 mPointerVelocityControl.reset();
4993
4994 down = false;
4995 hovering = false;
4996 }
4997
4998 dispatchPointerSimple(when, policyFlags, down, hovering);
4999}
5000
5001void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5002 abortPointerSimple(when, policyFlags);
5003
5004 mPointerVelocityControl.reset();
5005}
5006
5007void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5008 bool down, bool hovering) {
5009 int32_t metaState = getContext()->getGlobalMetaState();
5010
5011 if (mPointerController != NULL) {
5012 if (down || hovering) {
5013 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5014 mPointerController->clearSpots();
5015 mPointerController->setButtonState(mCurrentButtonState);
5016 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5017 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5018 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5019 }
5020 }
5021
5022 if (mPointerSimple.down && !down) {
5023 mPointerSimple.down = false;
5024
5025 // Send up.
5026 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5027 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5028 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5029 mOrientedXPrecision, mOrientedYPrecision,
5030 mPointerSimple.downTime);
5031 getListener()->notifyMotion(&args);
5032 }
5033
5034 if (mPointerSimple.hovering && !hovering) {
5035 mPointerSimple.hovering = false;
5036
5037 // Send hover exit.
5038 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5039 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5040 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5041 mOrientedXPrecision, mOrientedYPrecision,
5042 mPointerSimple.downTime);
5043 getListener()->notifyMotion(&args);
5044 }
5045
5046 if (down) {
5047 if (!mPointerSimple.down) {
5048 mPointerSimple.down = true;
5049 mPointerSimple.downTime = when;
5050
5051 // Send down.
5052 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5053 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5054 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5055 mOrientedXPrecision, mOrientedYPrecision,
5056 mPointerSimple.downTime);
5057 getListener()->notifyMotion(&args);
5058 }
5059
5060 // Send move.
5061 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5062 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5063 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5064 mOrientedXPrecision, mOrientedYPrecision,
5065 mPointerSimple.downTime);
5066 getListener()->notifyMotion(&args);
5067 }
5068
5069 if (hovering) {
5070 if (!mPointerSimple.hovering) {
5071 mPointerSimple.hovering = true;
5072
5073 // Send hover enter.
5074 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5075 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5076 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5077 mOrientedXPrecision, mOrientedYPrecision,
5078 mPointerSimple.downTime);
5079 getListener()->notifyMotion(&args);
5080 }
5081
5082 // Send hover move.
5083 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5084 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5085 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5086 mOrientedXPrecision, mOrientedYPrecision,
5087 mPointerSimple.downTime);
5088 getListener()->notifyMotion(&args);
5089 }
5090
5091 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5092 float vscroll = mCurrentRawVScroll;
5093 float hscroll = mCurrentRawHScroll;
5094 mWheelYVelocityControl.move(when, NULL, &vscroll);
5095 mWheelXVelocityControl.move(when, &hscroll, NULL);
5096
5097 // Send scroll.
5098 PointerCoords pointerCoords;
5099 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5100 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5101 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5102
5103 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5104 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5105 1, &mPointerSimple.currentProperties, &pointerCoords,
5106 mOrientedXPrecision, mOrientedYPrecision,
5107 mPointerSimple.downTime);
5108 getListener()->notifyMotion(&args);
5109 }
5110
5111 // Save state.
5112 if (down || hovering) {
5113 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5114 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5115 } else {
5116 mPointerSimple.reset();
5117 }
5118}
5119
5120void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5121 mPointerSimple.currentCoords.clear();
5122 mPointerSimple.currentProperties.clear();
5123
5124 dispatchPointerSimple(when, policyFlags, false, false);
5125}
5126
Jeff Brownace13b12011-03-09 17:39:48 -08005127void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005128 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5129 const PointerProperties* properties, const PointerCoords* coords,
5130 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005131 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5132 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005133 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005134 uint32_t pointerCount = 0;
5135 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005136 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005137 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005138 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005139 pointerCoords[pointerCount].copyFrom(coords[index]);
5140
5141 if (changedId >= 0 && id == uint32_t(changedId)) {
5142 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5143 }
5144
5145 pointerCount += 1;
5146 }
5147
Jeff Brownb6110c22011-04-01 16:15:13 -07005148 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005149
5150 if (changedId >= 0 && pointerCount == 1) {
5151 // Replace initial down and final up action.
5152 // We can compare the action without masking off the changed pointer index
5153 // because we know the index is 0.
5154 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5155 action = AMOTION_EVENT_ACTION_DOWN;
5156 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5157 action = AMOTION_EVENT_ACTION_UP;
5158 } else {
5159 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07005160 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005161 }
5162 }
5163
Jeff Brownbe1aa822011-07-27 16:04:54 -07005164 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005165 action, flags, metaState, buttonState, edgeFlags,
5166 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005167 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005168}
5169
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005170bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005171 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005172 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5173 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005174 bool changed = false;
5175 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005176 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005177 uint32_t inIndex = inIdToIndex[id];
5178 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005179
5180 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005181 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005182 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005183 PointerCoords& curOutCoords = outCoords[outIndex];
5184
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005185 if (curInProperties != curOutProperties) {
5186 curOutProperties.copyFrom(curInProperties);
5187 changed = true;
5188 }
5189
Jeff Brownace13b12011-03-09 17:39:48 -08005190 if (curInCoords != curOutCoords) {
5191 curOutCoords.copyFrom(curInCoords);
5192 changed = true;
5193 }
5194 }
5195 return changed;
5196}
5197
5198void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005199 if (mPointerController != NULL) {
5200 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5201 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005202}
5203
Jeff Brownbe1aa822011-07-27 16:04:54 -07005204bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5205 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5206 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005207}
5208
Jeff Brownbe1aa822011-07-27 16:04:54 -07005209const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005210 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005211 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005212 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005213 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005214
5215#if DEBUG_VIRTUAL_KEYS
5216 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
5217 "left=%d, top=%d, right=%d, bottom=%d",
5218 x, y,
5219 virtualKey.keyCode, virtualKey.scanCode,
5220 virtualKey.hitLeft, virtualKey.hitTop,
5221 virtualKey.hitRight, virtualKey.hitBottom);
5222#endif
5223
5224 if (virtualKey.isHit(x, y)) {
5225 return & virtualKey;
5226 }
5227 }
5228
5229 return NULL;
5230}
5231
Jeff Brownbe1aa822011-07-27 16:04:54 -07005232void TouchInputMapper::assignPointerIds() {
5233 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5234 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5235
5236 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005237
5238 if (currentPointerCount == 0) {
5239 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005240 return;
5241 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005242
Jeff Brownbe1aa822011-07-27 16:04:54 -07005243 if (lastPointerCount == 0) {
5244 // All pointers are new.
5245 for (uint32_t i = 0; i < currentPointerCount; i++) {
5246 uint32_t id = i;
5247 mCurrentRawPointerData.pointers[i].id = id;
5248 mCurrentRawPointerData.idToIndex[id] = i;
5249 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5250 }
5251 return;
5252 }
5253
5254 if (currentPointerCount == 1 && lastPointerCount == 1
5255 && mCurrentRawPointerData.pointers[0].toolType
5256 == mLastRawPointerData.pointers[0].toolType) {
5257 // Only one pointer and no change in count so it must have the same id as before.
5258 uint32_t id = mLastRawPointerData.pointers[0].id;
5259 mCurrentRawPointerData.pointers[0].id = id;
5260 mCurrentRawPointerData.idToIndex[id] = 0;
5261 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5262 return;
5263 }
5264
5265 // General case.
5266 // We build a heap of squared euclidean distances between current and last pointers
5267 // associated with the current and last pointer indices. Then, we find the best
5268 // match (by distance) for each current pointer.
5269 // The pointers must have the same tool type but it is possible for them to
5270 // transition from hovering to touching or vice-versa while retaining the same id.
5271 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5272
5273 uint32_t heapSize = 0;
5274 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5275 currentPointerIndex++) {
5276 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5277 lastPointerIndex++) {
5278 const RawPointerData::Pointer& currentPointer =
5279 mCurrentRawPointerData.pointers[currentPointerIndex];
5280 const RawPointerData::Pointer& lastPointer =
5281 mLastRawPointerData.pointers[lastPointerIndex];
5282 if (currentPointer.toolType == lastPointer.toolType) {
5283 int64_t deltaX = currentPointer.x - lastPointer.x;
5284 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005285
5286 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5287
5288 // Insert new element into the heap (sift up).
5289 heap[heapSize].currentPointerIndex = currentPointerIndex;
5290 heap[heapSize].lastPointerIndex = lastPointerIndex;
5291 heap[heapSize].distance = distance;
5292 heapSize += 1;
5293 }
5294 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005295 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005296
Jeff Brownbe1aa822011-07-27 16:04:54 -07005297 // Heapify
5298 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5299 startIndex -= 1;
5300 for (uint32_t parentIndex = startIndex; ;) {
5301 uint32_t childIndex = parentIndex * 2 + 1;
5302 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005303 break;
5304 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005305
5306 if (childIndex + 1 < heapSize
5307 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5308 childIndex += 1;
5309 }
5310
5311 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5312 break;
5313 }
5314
5315 swap(heap[parentIndex], heap[childIndex]);
5316 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005317 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005318 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005319
5320#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07005321 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
5322 for (size_t i = 0; i < heapSize; i++) {
5323 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5324 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5325 heap[i].distance);
5326 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005327#endif
5328
Jeff Brownbe1aa822011-07-27 16:04:54 -07005329 // Pull matches out by increasing order of distance.
5330 // To avoid reassigning pointers that have already been matched, the loop keeps track
5331 // of which last and current pointers have been matched using the matchedXXXBits variables.
5332 // It also tracks the used pointer id bits.
5333 BitSet32 matchedLastBits(0);
5334 BitSet32 matchedCurrentBits(0);
5335 BitSet32 usedIdBits(0);
5336 bool first = true;
5337 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5338 while (heapSize > 0) {
5339 if (first) {
5340 // The first time through the loop, we just consume the root element of
5341 // the heap (the one with smallest distance).
5342 first = false;
5343 } else {
5344 // Previous iterations consumed the root element of the heap.
5345 // Pop root element off of the heap (sift down).
5346 heap[0] = heap[heapSize];
5347 for (uint32_t parentIndex = 0; ;) {
5348 uint32_t childIndex = parentIndex * 2 + 1;
5349 if (childIndex >= heapSize) {
5350 break;
5351 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005352
Jeff Brownbe1aa822011-07-27 16:04:54 -07005353 if (childIndex + 1 < heapSize
5354 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5355 childIndex += 1;
5356 }
5357
5358 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5359 break;
5360 }
5361
5362 swap(heap[parentIndex], heap[childIndex]);
5363 parentIndex = childIndex;
5364 }
5365
5366#if DEBUG_POINTER_ASSIGNMENT
5367 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
5368 for (size_t i = 0; i < heapSize; i++) {
5369 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5370 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5371 heap[i].distance);
5372 }
5373#endif
5374 }
5375
5376 heapSize -= 1;
5377
5378 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5379 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5380
5381 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5382 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5383
5384 matchedCurrentBits.markBit(currentPointerIndex);
5385 matchedLastBits.markBit(lastPointerIndex);
5386
5387 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5388 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5389 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5390 mCurrentRawPointerData.markIdBit(id,
5391 mCurrentRawPointerData.isHovering(currentPointerIndex));
5392 usedIdBits.markBit(id);
5393
5394#if DEBUG_POINTER_ASSIGNMENT
5395 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
5396 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5397#endif
5398 break;
5399 }
5400 }
5401
5402 // Assign fresh ids to pointers that were not matched in the process.
5403 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5404 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5405 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5406
5407 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5408 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5409 mCurrentRawPointerData.markIdBit(id,
5410 mCurrentRawPointerData.isHovering(currentPointerIndex));
5411
5412#if DEBUG_POINTER_ASSIGNMENT
5413 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
5414 currentPointerIndex, id);
5415#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005416 }
5417}
5418
Jeff Brown6d0fec22010-07-23 21:28:06 -07005419int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005420 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5421 return AKEY_STATE_VIRTUAL;
5422 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005423
Jeff Brownbe1aa822011-07-27 16:04:54 -07005424 size_t numVirtualKeys = mVirtualKeys.size();
5425 for (size_t i = 0; i < numVirtualKeys; i++) {
5426 const VirtualKey& virtualKey = mVirtualKeys[i];
5427 if (virtualKey.keyCode == keyCode) {
5428 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005429 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005430 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005431
5432 return AKEY_STATE_UNKNOWN;
5433}
5434
5435int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005436 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5437 return AKEY_STATE_VIRTUAL;
5438 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005439
Jeff Brownbe1aa822011-07-27 16:04:54 -07005440 size_t numVirtualKeys = mVirtualKeys.size();
5441 for (size_t i = 0; i < numVirtualKeys; i++) {
5442 const VirtualKey& virtualKey = mVirtualKeys[i];
5443 if (virtualKey.scanCode == scanCode) {
5444 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005445 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005446 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005447
5448 return AKEY_STATE_UNKNOWN;
5449}
5450
5451bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5452 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005453 size_t numVirtualKeys = mVirtualKeys.size();
5454 for (size_t i = 0; i < numVirtualKeys; i++) {
5455 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005456
Jeff Brownbe1aa822011-07-27 16:04:54 -07005457 for (size_t i = 0; i < numCodes; i++) {
5458 if (virtualKey.keyCode == keyCodes[i]) {
5459 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005460 }
5461 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005462 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005463
5464 return true;
5465}
5466
5467
5468// --- SingleTouchInputMapper ---
5469
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005470SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5471 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005472}
5473
5474SingleTouchInputMapper::~SingleTouchInputMapper() {
5475}
5476
Jeff Brown65fd2512011-08-18 11:20:58 -07005477void SingleTouchInputMapper::reset(nsecs_t when) {
5478 mSingleTouchMotionAccumulator.reset(getDevice());
5479
5480 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005481}
5482
Jeff Brown6d0fec22010-07-23 21:28:06 -07005483void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005484 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005485
Jeff Brown65fd2512011-08-18 11:20:58 -07005486 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005487}
5488
Jeff Brown65fd2512011-08-18 11:20:58 -07005489void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005490 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005491 mCurrentRawPointerData.pointerCount = 1;
5492 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005493
Jeff Brown65fd2512011-08-18 11:20:58 -07005494 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5495 && (mTouchButtonAccumulator.isHovering()
5496 || (mRawPointerAxes.pressure.valid
5497 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005498 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005499
Jeff Brownbe1aa822011-07-27 16:04:54 -07005500 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005501 outPointer.id = 0;
5502 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5503 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5504 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5505 outPointer.touchMajor = 0;
5506 outPointer.touchMinor = 0;
5507 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5508 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5509 outPointer.orientation = 0;
5510 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005511 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5512 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005513 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5514 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5515 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5516 }
5517 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005518 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005519}
5520
Jeff Brownbe1aa822011-07-27 16:04:54 -07005521void SingleTouchInputMapper::configureRawPointerAxes() {
5522 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005523
Jeff Brownbe1aa822011-07-27 16:04:54 -07005524 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5525 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5526 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5527 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5528 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005529 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5530 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005531}
5532
5533
5534// --- MultiTouchInputMapper ---
5535
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005536MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005537 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005538}
5539
5540MultiTouchInputMapper::~MultiTouchInputMapper() {
5541}
5542
Jeff Brown65fd2512011-08-18 11:20:58 -07005543void MultiTouchInputMapper::reset(nsecs_t when) {
5544 mMultiTouchMotionAccumulator.reset(getDevice());
5545
Jeff Brown6894a292011-07-01 17:59:27 -07005546 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005547
Jeff Brown65fd2512011-08-18 11:20:58 -07005548 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005549}
5550
5551void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005552 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005553
Jeff Brown65fd2512011-08-18 11:20:58 -07005554 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005555}
5556
Jeff Brown65fd2512011-08-18 11:20:58 -07005557void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005558 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005559 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005560 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005561
Jeff Brown80fd47c2011-05-24 01:07:44 -07005562 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005563 const MultiTouchMotionAccumulator::Slot* inSlot =
5564 mMultiTouchMotionAccumulator.getSlot(inIndex);
5565 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005566 continue;
5567 }
5568
Jeff Brown80fd47c2011-05-24 01:07:44 -07005569 if (outCount >= MAX_POINTERS) {
5570#if DEBUG_POINTERS
5571 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5572 "ignoring the rest.",
5573 getDeviceName().string(), MAX_POINTERS);
5574#endif
5575 break; // too many fingers!
5576 }
5577
Jeff Brownbe1aa822011-07-27 16:04:54 -07005578 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005579 outPointer.x = inSlot->getX();
5580 outPointer.y = inSlot->getY();
5581 outPointer.pressure = inSlot->getPressure();
5582 outPointer.touchMajor = inSlot->getTouchMajor();
5583 outPointer.touchMinor = inSlot->getTouchMinor();
5584 outPointer.toolMajor = inSlot->getToolMajor();
5585 outPointer.toolMinor = inSlot->getToolMinor();
5586 outPointer.orientation = inSlot->getOrientation();
5587 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005588 outPointer.tiltX = 0;
5589 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005590
Jeff Brown49754db2011-07-01 17:37:58 -07005591 outPointer.toolType = inSlot->getToolType();
5592 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5593 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5594 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5595 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5596 }
Jeff Brown8d608662010-08-30 03:02:23 -07005597 }
5598
Jeff Brown65fd2512011-08-18 11:20:58 -07005599 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5600 && (mTouchButtonAccumulator.isHovering()
5601 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005602 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005603
Jeff Brown8d608662010-08-30 03:02:23 -07005604 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005605 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005606 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005607 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005608 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005609 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005610 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005611 if (mPointerTrackingIdMap[n] == trackingId) {
5612 id = n;
5613 }
5614 }
5615
5616 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005617 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005618 mPointerTrackingIdMap[id] = trackingId;
5619 }
5620 }
5621 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005622 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005623 mCurrentRawPointerData.clearIdBits();
5624 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005625 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005626 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005627 mCurrentRawPointerData.idToIndex[id] = outCount;
5628 mCurrentRawPointerData.markIdBit(id, isHovering);
5629 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005630 }
5631 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005632
Jeff Brown6d0fec22010-07-23 21:28:06 -07005633 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005634 }
5635
Jeff Brownbe1aa822011-07-27 16:04:54 -07005636 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005637 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005638
Jeff Brown65fd2512011-08-18 11:20:58 -07005639 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005640}
5641
Jeff Brownbe1aa822011-07-27 16:04:54 -07005642void MultiTouchInputMapper::configureRawPointerAxes() {
5643 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005644
Jeff Brownbe1aa822011-07-27 16:04:54 -07005645 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5646 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5647 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5648 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5649 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5650 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5651 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5652 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5653 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5654 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5655 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005656
Jeff Brownbe1aa822011-07-27 16:04:54 -07005657 if (mRawPointerAxes.trackingId.valid
5658 && mRawPointerAxes.slot.valid
5659 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5660 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005661 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005662 LOGW("MultiTouch Device %s reported %d slots but the framework "
5663 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005664 getDeviceName().string(), slotCount, MAX_SLOTS);
5665 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005666 }
Jeff Brown49754db2011-07-01 17:37:58 -07005667 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005668 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005669 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005670 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005671}
5672
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005673
Jeff Browncb1404e2011-01-15 18:14:15 -08005674// --- JoystickInputMapper ---
5675
5676JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5677 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005678}
5679
5680JoystickInputMapper::~JoystickInputMapper() {
5681}
5682
5683uint32_t JoystickInputMapper::getSources() {
5684 return AINPUT_SOURCE_JOYSTICK;
5685}
5686
5687void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5688 InputMapper::populateDeviceInfo(info);
5689
Jeff Brown6f2fba42011-02-19 01:08:02 -08005690 for (size_t i = 0; i < mAxes.size(); i++) {
5691 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005692 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5693 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005694 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005695 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5696 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005697 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005698 }
5699}
5700
5701void JoystickInputMapper::dump(String8& dump) {
5702 dump.append(INDENT2 "Joystick Input Mapper:\n");
5703
Jeff Brown6f2fba42011-02-19 01:08:02 -08005704 dump.append(INDENT3 "Axes:\n");
5705 size_t numAxes = mAxes.size();
5706 for (size_t i = 0; i < numAxes; i++) {
5707 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005708 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005709 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005710 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005711 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005712 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005713 }
Jeff Brown85297452011-03-04 13:07:49 -08005714 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5715 label = getAxisLabel(axis.axisInfo.highAxis);
5716 if (label) {
5717 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5718 } else {
5719 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5720 axis.axisInfo.splitValue);
5721 }
5722 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5723 dump.append(" (invert)");
5724 }
5725
5726 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5727 axis.min, axis.max, axis.flat, axis.fuzz);
5728 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5729 "highScale=%0.5f, highOffset=%0.5f\n",
5730 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005731 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5732 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005733 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005734 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005735 }
5736}
5737
Jeff Brown65fd2512011-08-18 11:20:58 -07005738void JoystickInputMapper::configure(nsecs_t when,
5739 const InputReaderConfiguration* config, uint32_t changes) {
5740 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005741
Jeff Brown474dcb52011-06-14 20:22:50 -07005742 if (!changes) { // first time only
5743 // Collect all axes.
5744 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5745 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005746 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005747 if (rawAxisInfo.valid) {
5748 // Map axis.
5749 AxisInfo axisInfo;
5750 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5751 if (!explicitlyMapped) {
5752 // Axis is not explicitly mapped, will choose a generic axis later.
5753 axisInfo.mode = AxisInfo::MODE_NORMAL;
5754 axisInfo.axis = -1;
5755 }
5756
5757 // Apply flat override.
5758 int32_t rawFlat = axisInfo.flatOverride < 0
5759 ? rawAxisInfo.flat : axisInfo.flatOverride;
5760
5761 // Calculate scaling factors and limits.
5762 Axis axis;
5763 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5764 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5765 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5766 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5767 scale, 0.0f, highScale, 0.0f,
5768 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5769 } else if (isCenteredAxis(axisInfo.axis)) {
5770 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5771 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5772 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5773 scale, offset, scale, offset,
5774 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5775 } else {
5776 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5777 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5778 scale, 0.0f, scale, 0.0f,
5779 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5780 }
5781
5782 // To eliminate noise while the joystick is at rest, filter out small variations
5783 // in axis values up front.
5784 axis.filter = axis.flat * 0.25f;
5785
5786 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005787 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005788 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005789
Jeff Brown474dcb52011-06-14 20:22:50 -07005790 // If there are too many axes, start dropping them.
5791 // Prefer to keep explicitly mapped axes.
5792 if (mAxes.size() > PointerCoords::MAX_AXES) {
5793 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5794 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5795 pruneAxes(true);
5796 pruneAxes(false);
5797 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005798
Jeff Brown474dcb52011-06-14 20:22:50 -07005799 // Assign generic axis ids to remaining axes.
5800 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5801 size_t numAxes = mAxes.size();
5802 for (size_t i = 0; i < numAxes; i++) {
5803 Axis& axis = mAxes.editValueAt(i);
5804 if (axis.axisInfo.axis < 0) {
5805 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5806 && haveAxis(nextGenericAxisId)) {
5807 nextGenericAxisId += 1;
5808 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005809
Jeff Brown474dcb52011-06-14 20:22:50 -07005810 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5811 axis.axisInfo.axis = nextGenericAxisId;
5812 nextGenericAxisId += 1;
5813 } else {
5814 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5815 "have already been assigned to other axes.",
5816 getDeviceName().string(), mAxes.keyAt(i));
5817 mAxes.removeItemsAt(i--);
5818 numAxes -= 1;
5819 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005820 }
5821 }
5822 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005823}
5824
Jeff Brown85297452011-03-04 13:07:49 -08005825bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005826 size_t numAxes = mAxes.size();
5827 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005828 const Axis& axis = mAxes.valueAt(i);
5829 if (axis.axisInfo.axis == axisId
5830 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5831 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005832 return true;
5833 }
5834 }
5835 return false;
5836}
Jeff Browncb1404e2011-01-15 18:14:15 -08005837
Jeff Brown6f2fba42011-02-19 01:08:02 -08005838void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5839 size_t i = mAxes.size();
5840 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5841 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5842 continue;
5843 }
5844 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5845 getDeviceName().string(), mAxes.keyAt(i));
5846 mAxes.removeItemsAt(i);
5847 }
5848}
5849
5850bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5851 switch (axis) {
5852 case AMOTION_EVENT_AXIS_X:
5853 case AMOTION_EVENT_AXIS_Y:
5854 case AMOTION_EVENT_AXIS_Z:
5855 case AMOTION_EVENT_AXIS_RX:
5856 case AMOTION_EVENT_AXIS_RY:
5857 case AMOTION_EVENT_AXIS_RZ:
5858 case AMOTION_EVENT_AXIS_HAT_X:
5859 case AMOTION_EVENT_AXIS_HAT_Y:
5860 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005861 case AMOTION_EVENT_AXIS_RUDDER:
5862 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005863 return true;
5864 default:
5865 return false;
5866 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005867}
5868
Jeff Brown65fd2512011-08-18 11:20:58 -07005869void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005870 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005871 size_t numAxes = mAxes.size();
5872 for (size_t i = 0; i < numAxes; i++) {
5873 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005874 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005875 }
5876
Jeff Brown65fd2512011-08-18 11:20:58 -07005877 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005878}
5879
5880void JoystickInputMapper::process(const RawEvent* rawEvent) {
5881 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005882 case EV_ABS: {
5883 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5884 if (index >= 0) {
5885 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005886 float newValue, highNewValue;
5887 switch (axis.axisInfo.mode) {
5888 case AxisInfo::MODE_INVERT:
5889 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5890 * axis.scale + axis.offset;
5891 highNewValue = 0.0f;
5892 break;
5893 case AxisInfo::MODE_SPLIT:
5894 if (rawEvent->value < axis.axisInfo.splitValue) {
5895 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5896 * axis.scale + axis.offset;
5897 highNewValue = 0.0f;
5898 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5899 newValue = 0.0f;
5900 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5901 * axis.highScale + axis.highOffset;
5902 } else {
5903 newValue = 0.0f;
5904 highNewValue = 0.0f;
5905 }
5906 break;
5907 default:
5908 newValue = rawEvent->value * axis.scale + axis.offset;
5909 highNewValue = 0.0f;
5910 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005911 }
Jeff Brown85297452011-03-04 13:07:49 -08005912 axis.newValue = newValue;
5913 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005914 }
5915 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005916 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005917
5918 case EV_SYN:
5919 switch (rawEvent->scanCode) {
5920 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005921 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005922 break;
5923 }
5924 break;
5925 }
5926}
5927
Jeff Brown6f2fba42011-02-19 01:08:02 -08005928void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005929 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005930 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005931 }
5932
5933 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005934 int32_t buttonState = 0;
5935
5936 PointerProperties pointerProperties;
5937 pointerProperties.clear();
5938 pointerProperties.id = 0;
5939 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005940
Jeff Brown6f2fba42011-02-19 01:08:02 -08005941 PointerCoords pointerCoords;
5942 pointerCoords.clear();
5943
5944 size_t numAxes = mAxes.size();
5945 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005946 const Axis& axis = mAxes.valueAt(i);
5947 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5948 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5949 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5950 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005951 }
5952
Jeff Brown56194eb2011-03-02 19:23:13 -08005953 // Moving a joystick axis should not wake the devide because joysticks can
5954 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5955 // button will likely wake the device.
5956 // TODO: Use the input device configuration to control this behavior more finely.
5957 uint32_t policyFlags = 0;
5958
Jeff Brownbe1aa822011-07-27 16:04:54 -07005959 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005960 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5961 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005962 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005963}
5964
Jeff Brown85297452011-03-04 13:07:49 -08005965bool JoystickInputMapper::filterAxes(bool force) {
5966 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005967 size_t numAxes = mAxes.size();
5968 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005969 Axis& axis = mAxes.editValueAt(i);
5970 if (force || hasValueChangedSignificantly(axis.filter,
5971 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5972 axis.currentValue = axis.newValue;
5973 atLeastOneSignificantChange = true;
5974 }
5975 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5976 if (force || hasValueChangedSignificantly(axis.filter,
5977 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5978 axis.highCurrentValue = axis.highNewValue;
5979 atLeastOneSignificantChange = true;
5980 }
5981 }
5982 }
5983 return atLeastOneSignificantChange;
5984}
5985
5986bool JoystickInputMapper::hasValueChangedSignificantly(
5987 float filter, float newValue, float currentValue, float min, float max) {
5988 if (newValue != currentValue) {
5989 // Filter out small changes in value unless the value is converging on the axis
5990 // bounds or center point. This is intended to reduce the amount of information
5991 // sent to applications by particularly noisy joysticks (such as PS3).
5992 if (fabs(newValue - currentValue) > filter
5993 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5994 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5995 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5996 return true;
5997 }
5998 }
5999 return false;
6000}
6001
6002bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6003 float filter, float newValue, float currentValue, float thresholdValue) {
6004 float newDistance = fabs(newValue - thresholdValue);
6005 if (newDistance < filter) {
6006 float oldDistance = fabs(currentValue - thresholdValue);
6007 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006008 return true;
6009 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006010 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006011 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006012}
6013
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006014} // namespace android