blob: 9a73527db9d308c2400b3af20c05fca2386b365c [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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brownfd035822010-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 Brown46b9ac02010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -0700364 return;
365 }
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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) {
Jeff Brown9ee285a2011-08-31 12:56:34 -0700393 InputDevice* device = new InputDevice(&mContext, deviceId, name, classes);
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800647 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
648 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
649 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
650 if (currentResult >= AKEY_STATE_DOWN) {
651 return currentResult;
652 } else if (currentResult == AKEY_STATE_UP) {
653 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700654 }
655 }
656 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700657 }
658 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700659}
660
661bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
662 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700663 AutoMutex _l(mLock);
664
Jeff Brown6d0fec22010-07-23 21:28:06 -0700665 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700666 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700667}
668
Jeff Brownbe1aa822011-07-27 16:04:54 -0700669bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
670 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
671 bool result = false;
672 if (deviceId >= 0) {
673 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
674 if (deviceIndex >= 0) {
675 InputDevice* device = mDevices.valueAt(deviceIndex);
676 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
677 result = device->markSupportedKeyCodes(sourceMask,
678 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700679 }
680 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700681 } else {
682 size_t numDevices = mDevices.size();
683 for (size_t i = 0; i < numDevices; i++) {
684 InputDevice* device = mDevices.valueAt(i);
685 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
686 result |= device->markSupportedKeyCodes(sourceMask,
687 numCodes, keyCodes, outFlags);
688 }
689 }
690 }
691 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700692}
693
Jeff Brown474dcb52011-06-14 20:22:50 -0700694void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700695 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700696
Jeff Brownbe1aa822011-07-27 16:04:54 -0700697 if (changes) {
698 bool needWake = !mConfigurationChangesToRefresh;
699 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700700
701 if (needWake) {
702 mEventHub->wake();
703 }
704 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700705}
706
Jeff Brownb88102f2010-09-08 11:49:43 -0700707void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700708 AutoMutex _l(mLock);
709
Jeff Brownf2f48712010-10-01 17:46:21 -0700710 mEventHub->dump(dump);
711 dump.append("\n");
712
713 dump.append("Input Reader State:\n");
714
Jeff Brownbe1aa822011-07-27 16:04:54 -0700715 for (size_t i = 0; i < mDevices.size(); i++) {
716 mDevices.valueAt(i)->dump(dump);
717 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700718
719 dump.append(INDENT "Configuration:\n");
720 dump.append(INDENT2 "ExcludedDeviceNames: [");
721 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
722 if (i != 0) {
723 dump.append(", ");
724 }
725 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
726 }
727 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700728 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
729 mConfig.virtualKeyQuietTime * 0.000001f);
730
Jeff Brown19c97d42011-06-01 12:33:19 -0700731 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
732 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
733 mConfig.pointerVelocityControlParameters.scale,
734 mConfig.pointerVelocityControlParameters.lowThreshold,
735 mConfig.pointerVelocityControlParameters.highThreshold,
736 mConfig.pointerVelocityControlParameters.acceleration);
737
738 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
739 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
740 mConfig.wheelVelocityControlParameters.scale,
741 mConfig.wheelVelocityControlParameters.lowThreshold,
742 mConfig.wheelVelocityControlParameters.highThreshold,
743 mConfig.wheelVelocityControlParameters.acceleration);
744
Jeff Brown214eaf42011-05-26 19:17:02 -0700745 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700746 dump.appendFormat(INDENT3 "Enabled: %s\n",
747 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700748 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
749 mConfig.pointerGestureQuietInterval * 0.000001f);
750 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
751 mConfig.pointerGestureDragMinSwitchSpeed);
752 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
753 mConfig.pointerGestureTapInterval * 0.000001f);
754 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
755 mConfig.pointerGestureTapDragInterval * 0.000001f);
756 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
757 mConfig.pointerGestureTapSlop);
758 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
759 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba2011-06-06 19:23:05 -0700760 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
761 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700762 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
763 mConfig.pointerGestureSwipeTransitionAngleCosine);
764 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
765 mConfig.pointerGestureSwipeMaxWidthRatio);
766 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
767 mConfig.pointerGestureMovementSpeedRatio);
768 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
769 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700770}
771
Jeff Brown89ef0722011-08-10 16:25:21 -0700772void InputReader::monitor() {
773 // Acquire and release the lock to ensure that the reader has not deadlocked.
774 mLock.lock();
775 mLock.unlock();
776
777 // Check the EventHub
778 mEventHub->monitor();
779}
780
Jeff Brown6d0fec22010-07-23 21:28:06 -0700781
Jeff Brownbe1aa822011-07-27 16:04:54 -0700782// --- InputReader::ContextImpl ---
783
784InputReader::ContextImpl::ContextImpl(InputReader* reader) :
785 mReader(reader) {
786}
787
788void InputReader::ContextImpl::updateGlobalMetaState() {
789 // lock is already held by the input loop
790 mReader->updateGlobalMetaStateLocked();
791}
792
793int32_t InputReader::ContextImpl::getGlobalMetaState() {
794 // lock is already held by the input loop
795 return mReader->getGlobalMetaStateLocked();
796}
797
798void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
799 // lock is already held by the input loop
800 mReader->disableVirtualKeysUntilLocked(time);
801}
802
803bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
804 InputDevice* device, int32_t keyCode, int32_t scanCode) {
805 // lock is already held by the input loop
806 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
807}
808
809void InputReader::ContextImpl::fadePointer() {
810 // lock is already held by the input loop
811 mReader->fadePointerLocked();
812}
813
814void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
815 // lock is already held by the input loop
816 mReader->requestTimeoutAtTimeLocked(when);
817}
818
819InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
820 return mReader->mPolicy.get();
821}
822
823InputListenerInterface* InputReader::ContextImpl::getListener() {
824 return mReader->mQueuedListener.get();
825}
826
827EventHubInterface* InputReader::ContextImpl::getEventHub() {
828 return mReader->mEventHub.get();
829}
830
831
Jeff Brown6d0fec22010-07-23 21:28:06 -0700832// --- InputReaderThread ---
833
834InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
835 Thread(/*canCallJava*/ true), mReader(reader) {
836}
837
838InputReaderThread::~InputReaderThread() {
839}
840
841bool InputReaderThread::threadLoop() {
842 mReader->loopOnce();
843 return true;
844}
845
846
847// --- InputDevice ---
848
Jeff Brown9ee285a2011-08-31 12:56:34 -0700849InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name,
850 uint32_t classes) :
851 mContext(context), mId(id), mName(name), mClasses(classes),
852 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700853}
854
855InputDevice::~InputDevice() {
856 size_t numMappers = mMappers.size();
857 for (size_t i = 0; i < numMappers; i++) {
858 delete mMappers[i];
859 }
860 mMappers.clear();
861}
862
Jeff Brownef3d7e82010-09-30 14:33:04 -0700863void InputDevice::dump(String8& dump) {
864 InputDeviceInfo deviceInfo;
865 getDeviceInfo(& deviceInfo);
866
Jeff Brown90655042010-12-02 13:50:46 -0800867 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700868 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800869 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700870 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
871 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800872
Jeff Brownefd32662011-03-08 15:13:06 -0800873 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800874 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700875 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800876 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800877 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
878 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800879 char name[32];
880 if (label) {
881 strncpy(name, label, sizeof(name));
882 name[sizeof(name) - 1] = '\0';
883 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800884 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800885 }
Jeff Brownefd32662011-03-08 15:13:06 -0800886 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
887 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
888 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800889 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700890 }
891
892 size_t numMappers = mMappers.size();
893 for (size_t i = 0; i < numMappers; i++) {
894 InputMapper* mapper = mMappers[i];
895 mapper->dump(dump);
896 }
897}
898
Jeff Brown6d0fec22010-07-23 21:28:06 -0700899void InputDevice::addMapper(InputMapper* mapper) {
900 mMappers.add(mapper);
901}
902
Jeff Brown65fd2512011-08-18 11:20:58 -0700903void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700904 mSources = 0;
905
Jeff Brown474dcb52011-06-14 20:22:50 -0700906 if (!isIgnored()) {
907 if (!changes) { // first time only
908 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
909 }
910
911 size_t numMappers = mMappers.size();
912 for (size_t i = 0; i < numMappers; i++) {
913 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700914 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700915 mSources |= mapper->getSources();
916 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700917 }
918}
919
Jeff Brown65fd2512011-08-18 11:20:58 -0700920void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700921 size_t numMappers = mMappers.size();
922 for (size_t i = 0; i < numMappers; i++) {
923 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700924 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700925 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700926
927 mContext->updateGlobalMetaState();
928
929 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700930}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700931
Jeff Brownb7198742011-03-18 18:14:26 -0700932void InputDevice::process(const RawEvent* rawEvents, size_t count) {
933 // Process all of the events in order for each mapper.
934 // We cannot simply ask each mapper to process them in bulk because mappers may
935 // have side-effects that must be interleaved. For example, joystick movement events and
936 // gamepad button presses are handled by different mappers but they should be dispatched
937 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700938 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700939 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
940#if DEBUG_RAW_EVENTS
941 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700942 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700943 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
944 rawEvent->value, rawEvent->flags);
945#endif
946
Jeff Brown80fd47c2011-05-24 01:07:44 -0700947 if (mDropUntilNextSync) {
948 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
949 mDropUntilNextSync = false;
950#if DEBUG_RAW_EVENTS
951 LOGD("Recovered from input event buffer overrun.");
952#endif
953 } else {
954#if DEBUG_RAW_EVENTS
955 LOGD("Dropped input event while waiting for next input sync.");
956#endif
957 }
958 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
959 LOGI("Detected input event buffer overrun for device %s.", mName.string());
960 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700961 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700962 } else {
963 for (size_t i = 0; i < numMappers; i++) {
964 InputMapper* mapper = mMappers[i];
965 mapper->process(rawEvent);
966 }
Jeff Brownb7198742011-03-18 18:14:26 -0700967 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700968 }
969}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700970
Jeff Brownaa3855d2011-03-17 01:34:19 -0700971void InputDevice::timeoutExpired(nsecs_t when) {
972 size_t numMappers = mMappers.size();
973 for (size_t i = 0; i < numMappers; i++) {
974 InputMapper* mapper = mMappers[i];
975 mapper->timeoutExpired(when);
976 }
977}
978
Jeff Brown6d0fec22010-07-23 21:28:06 -0700979void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
980 outDeviceInfo->initialize(mId, mName);
981
982 size_t numMappers = mMappers.size();
983 for (size_t i = 0; i < numMappers; i++) {
984 InputMapper* mapper = mMappers[i];
985 mapper->populateDeviceInfo(outDeviceInfo);
986 }
987}
988
989int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
990 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
991}
992
993int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
994 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
995}
996
997int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
998 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
999}
1000
1001int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1002 int32_t result = AKEY_STATE_UNKNOWN;
1003 size_t numMappers = mMappers.size();
1004 for (size_t i = 0; i < numMappers; i++) {
1005 InputMapper* mapper = mMappers[i];
1006 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001007 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1008 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1009 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1010 if (currentResult >= AKEY_STATE_DOWN) {
1011 return currentResult;
1012 } else if (currentResult == AKEY_STATE_UP) {
1013 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001014 }
1015 }
1016 }
1017 return result;
1018}
1019
1020bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1021 const int32_t* keyCodes, uint8_t* outFlags) {
1022 bool result = false;
1023 size_t numMappers = mMappers.size();
1024 for (size_t i = 0; i < numMappers; i++) {
1025 InputMapper* mapper = mMappers[i];
1026 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1027 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1028 }
1029 }
1030 return result;
1031}
1032
1033int32_t InputDevice::getMetaState() {
1034 int32_t result = 0;
1035 size_t numMappers = mMappers.size();
1036 for (size_t i = 0; i < numMappers; i++) {
1037 InputMapper* mapper = mMappers[i];
1038 result |= mapper->getMetaState();
1039 }
1040 return result;
1041}
1042
Jeff Brown05dc66a2011-03-02 14:41:58 -08001043void InputDevice::fadePointer() {
1044 size_t numMappers = mMappers.size();
1045 for (size_t i = 0; i < numMappers; i++) {
1046 InputMapper* mapper = mMappers[i];
1047 mapper->fadePointer();
1048 }
1049}
1050
Jeff Brown65fd2512011-08-18 11:20:58 -07001051void InputDevice::notifyReset(nsecs_t when) {
1052 NotifyDeviceResetArgs args(when, mId);
1053 mContext->getListener()->notifyDeviceReset(&args);
1054}
1055
Jeff Brown6d0fec22010-07-23 21:28:06 -07001056
Jeff Brown49754db2011-07-01 17:37:58 -07001057// --- CursorButtonAccumulator ---
1058
1059CursorButtonAccumulator::CursorButtonAccumulator() {
1060 clearButtons();
1061}
1062
Jeff Brown65fd2512011-08-18 11:20:58 -07001063void CursorButtonAccumulator::reset(InputDevice* device) {
1064 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1065 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1066 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1067 mBtnBack = device->isKeyPressed(BTN_BACK);
1068 mBtnSide = device->isKeyPressed(BTN_SIDE);
1069 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1070 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1071 mBtnTask = device->isKeyPressed(BTN_TASK);
1072}
1073
Jeff Brown49754db2011-07-01 17:37:58 -07001074void CursorButtonAccumulator::clearButtons() {
1075 mBtnLeft = 0;
1076 mBtnRight = 0;
1077 mBtnMiddle = 0;
1078 mBtnBack = 0;
1079 mBtnSide = 0;
1080 mBtnForward = 0;
1081 mBtnExtra = 0;
1082 mBtnTask = 0;
1083}
1084
1085void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1086 if (rawEvent->type == EV_KEY) {
1087 switch (rawEvent->scanCode) {
1088 case BTN_LEFT:
1089 mBtnLeft = rawEvent->value;
1090 break;
1091 case BTN_RIGHT:
1092 mBtnRight = rawEvent->value;
1093 break;
1094 case BTN_MIDDLE:
1095 mBtnMiddle = rawEvent->value;
1096 break;
1097 case BTN_BACK:
1098 mBtnBack = rawEvent->value;
1099 break;
1100 case BTN_SIDE:
1101 mBtnSide = rawEvent->value;
1102 break;
1103 case BTN_FORWARD:
1104 mBtnForward = rawEvent->value;
1105 break;
1106 case BTN_EXTRA:
1107 mBtnExtra = rawEvent->value;
1108 break;
1109 case BTN_TASK:
1110 mBtnTask = rawEvent->value;
1111 break;
1112 }
1113 }
1114}
1115
1116uint32_t CursorButtonAccumulator::getButtonState() const {
1117 uint32_t result = 0;
1118 if (mBtnLeft) {
1119 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1120 }
1121 if (mBtnRight) {
1122 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1123 }
1124 if (mBtnMiddle) {
1125 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1126 }
1127 if (mBtnBack || mBtnSide) {
1128 result |= AMOTION_EVENT_BUTTON_BACK;
1129 }
1130 if (mBtnForward || mBtnExtra) {
1131 result |= AMOTION_EVENT_BUTTON_FORWARD;
1132 }
1133 return result;
1134}
1135
1136
1137// --- CursorMotionAccumulator ---
1138
Jeff Brown65fd2512011-08-18 11:20:58 -07001139CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001140 clearRelativeAxes();
1141}
1142
Jeff Brown65fd2512011-08-18 11:20:58 -07001143void CursorMotionAccumulator::reset(InputDevice* device) {
1144 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001145}
1146
1147void CursorMotionAccumulator::clearRelativeAxes() {
1148 mRelX = 0;
1149 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001150}
1151
1152void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1153 if (rawEvent->type == EV_REL) {
1154 switch (rawEvent->scanCode) {
1155 case REL_X:
1156 mRelX = rawEvent->value;
1157 break;
1158 case REL_Y:
1159 mRelY = rawEvent->value;
1160 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001161 }
1162 }
1163}
1164
1165void CursorMotionAccumulator::finishSync() {
1166 clearRelativeAxes();
1167}
1168
1169
1170// --- CursorScrollAccumulator ---
1171
1172CursorScrollAccumulator::CursorScrollAccumulator() :
1173 mHaveRelWheel(false), mHaveRelHWheel(false) {
1174 clearRelativeAxes();
1175}
1176
1177void CursorScrollAccumulator::configure(InputDevice* device) {
1178 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1179 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1180}
1181
1182void CursorScrollAccumulator::reset(InputDevice* device) {
1183 clearRelativeAxes();
1184}
1185
1186void CursorScrollAccumulator::clearRelativeAxes() {
1187 mRelWheel = 0;
1188 mRelHWheel = 0;
1189}
1190
1191void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1192 if (rawEvent->type == EV_REL) {
1193 switch (rawEvent->scanCode) {
Jeff Brown49754db2011-07-01 17:37:58 -07001194 case REL_WHEEL:
1195 mRelWheel = rawEvent->value;
1196 break;
1197 case REL_HWHEEL:
1198 mRelHWheel = rawEvent->value;
1199 break;
1200 }
1201 }
1202}
1203
Jeff Brown65fd2512011-08-18 11:20:58 -07001204void CursorScrollAccumulator::finishSync() {
1205 clearRelativeAxes();
1206}
1207
Jeff Brown49754db2011-07-01 17:37:58 -07001208
1209// --- TouchButtonAccumulator ---
1210
1211TouchButtonAccumulator::TouchButtonAccumulator() :
1212 mHaveBtnTouch(false) {
1213 clearButtons();
1214}
1215
1216void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001217 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1218}
1219
1220void TouchButtonAccumulator::reset(InputDevice* device) {
1221 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1222 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1223 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1224 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1225 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1226 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1227 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1228 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1229 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1230 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1231 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001232 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1233 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1234 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001235}
1236
1237void TouchButtonAccumulator::clearButtons() {
1238 mBtnTouch = 0;
1239 mBtnStylus = 0;
1240 mBtnStylus2 = 0;
1241 mBtnToolFinger = 0;
1242 mBtnToolPen = 0;
1243 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001244 mBtnToolBrush = 0;
1245 mBtnToolPencil = 0;
1246 mBtnToolAirbrush = 0;
1247 mBtnToolMouse = 0;
1248 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001249 mBtnToolDoubleTap = 0;
1250 mBtnToolTripleTap = 0;
1251 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001252}
1253
1254void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1255 if (rawEvent->type == EV_KEY) {
1256 switch (rawEvent->scanCode) {
1257 case BTN_TOUCH:
1258 mBtnTouch = rawEvent->value;
1259 break;
1260 case BTN_STYLUS:
1261 mBtnStylus = rawEvent->value;
1262 break;
1263 case BTN_STYLUS2:
1264 mBtnStylus2 = rawEvent->value;
1265 break;
1266 case BTN_TOOL_FINGER:
1267 mBtnToolFinger = rawEvent->value;
1268 break;
1269 case BTN_TOOL_PEN:
1270 mBtnToolPen = rawEvent->value;
1271 break;
1272 case BTN_TOOL_RUBBER:
1273 mBtnToolRubber = rawEvent->value;
1274 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001275 case BTN_TOOL_BRUSH:
1276 mBtnToolBrush = rawEvent->value;
1277 break;
1278 case BTN_TOOL_PENCIL:
1279 mBtnToolPencil = rawEvent->value;
1280 break;
1281 case BTN_TOOL_AIRBRUSH:
1282 mBtnToolAirbrush = rawEvent->value;
1283 break;
1284 case BTN_TOOL_MOUSE:
1285 mBtnToolMouse = rawEvent->value;
1286 break;
1287 case BTN_TOOL_LENS:
1288 mBtnToolLens = rawEvent->value;
1289 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001290 case BTN_TOOL_DOUBLETAP:
1291 mBtnToolDoubleTap = rawEvent->value;
1292 break;
1293 case BTN_TOOL_TRIPLETAP:
1294 mBtnToolTripleTap = rawEvent->value;
1295 break;
1296 case BTN_TOOL_QUADTAP:
1297 mBtnToolQuadTap = rawEvent->value;
1298 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001299 }
1300 }
1301}
1302
1303uint32_t TouchButtonAccumulator::getButtonState() const {
1304 uint32_t result = 0;
1305 if (mBtnStylus) {
1306 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1307 }
1308 if (mBtnStylus2) {
1309 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1310 }
1311 return result;
1312}
1313
1314int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001315 if (mBtnToolMouse || mBtnToolLens) {
1316 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1317 }
Jeff Brown49754db2011-07-01 17:37:58 -07001318 if (mBtnToolRubber) {
1319 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1320 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001321 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001322 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1323 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001324 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001325 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1326 }
1327 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1328}
1329
Jeff Brownd87c6d52011-08-10 14:55:59 -07001330bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001331 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1332 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001333 || mBtnToolMouse || mBtnToolLens
1334 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001335}
1336
1337bool TouchButtonAccumulator::isHovering() const {
1338 return mHaveBtnTouch && !mBtnTouch;
1339}
1340
1341
Jeff Brownbe1aa822011-07-27 16:04:54 -07001342// --- RawPointerAxes ---
1343
1344RawPointerAxes::RawPointerAxes() {
1345 clear();
1346}
1347
1348void RawPointerAxes::clear() {
1349 x.clear();
1350 y.clear();
1351 pressure.clear();
1352 touchMajor.clear();
1353 touchMinor.clear();
1354 toolMajor.clear();
1355 toolMinor.clear();
1356 orientation.clear();
1357 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001358 tiltX.clear();
1359 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001360 trackingId.clear();
1361 slot.clear();
1362}
1363
1364
1365// --- RawPointerData ---
1366
1367RawPointerData::RawPointerData() {
1368 clear();
1369}
1370
1371void RawPointerData::clear() {
1372 pointerCount = 0;
1373 clearIdBits();
1374}
1375
1376void RawPointerData::copyFrom(const RawPointerData& other) {
1377 pointerCount = other.pointerCount;
1378 hoveringIdBits = other.hoveringIdBits;
1379 touchingIdBits = other.touchingIdBits;
1380
1381 for (uint32_t i = 0; i < pointerCount; i++) {
1382 pointers[i] = other.pointers[i];
1383
1384 int id = pointers[i].id;
1385 idToIndex[id] = other.idToIndex[id];
1386 }
1387}
1388
1389void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1390 float x = 0, y = 0;
1391 uint32_t count = touchingIdBits.count();
1392 if (count) {
1393 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1394 uint32_t id = idBits.clearFirstMarkedBit();
1395 const Pointer& pointer = pointerForId(id);
1396 x += pointer.x;
1397 y += pointer.y;
1398 }
1399 x /= count;
1400 y /= count;
1401 }
1402 *outX = x;
1403 *outY = y;
1404}
1405
1406
1407// --- CookedPointerData ---
1408
1409CookedPointerData::CookedPointerData() {
1410 clear();
1411}
1412
1413void CookedPointerData::clear() {
1414 pointerCount = 0;
1415 hoveringIdBits.clear();
1416 touchingIdBits.clear();
1417}
1418
1419void CookedPointerData::copyFrom(const CookedPointerData& other) {
1420 pointerCount = other.pointerCount;
1421 hoveringIdBits = other.hoveringIdBits;
1422 touchingIdBits = other.touchingIdBits;
1423
1424 for (uint32_t i = 0; i < pointerCount; i++) {
1425 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1426 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1427
1428 int id = pointerProperties[i].id;
1429 idToIndex[id] = other.idToIndex[id];
1430 }
1431}
1432
1433
Jeff Brown49754db2011-07-01 17:37:58 -07001434// --- SingleTouchMotionAccumulator ---
1435
1436SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1437 clearAbsoluteAxes();
1438}
1439
Jeff Brown65fd2512011-08-18 11:20:58 -07001440void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1441 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1442 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1443 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1444 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1445 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1446 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1447 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1448}
1449
Jeff Brown49754db2011-07-01 17:37:58 -07001450void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1451 mAbsX = 0;
1452 mAbsY = 0;
1453 mAbsPressure = 0;
1454 mAbsToolWidth = 0;
1455 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001456 mAbsTiltX = 0;
1457 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001458}
1459
1460void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1461 if (rawEvent->type == EV_ABS) {
1462 switch (rawEvent->scanCode) {
1463 case ABS_X:
1464 mAbsX = rawEvent->value;
1465 break;
1466 case ABS_Y:
1467 mAbsY = rawEvent->value;
1468 break;
1469 case ABS_PRESSURE:
1470 mAbsPressure = rawEvent->value;
1471 break;
1472 case ABS_TOOL_WIDTH:
1473 mAbsToolWidth = rawEvent->value;
1474 break;
1475 case ABS_DISTANCE:
1476 mAbsDistance = rawEvent->value;
1477 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001478 case ABS_TILT_X:
1479 mAbsTiltX = rawEvent->value;
1480 break;
1481 case ABS_TILT_Y:
1482 mAbsTiltY = rawEvent->value;
1483 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001484 }
1485 }
1486}
1487
1488
1489// --- MultiTouchMotionAccumulator ---
1490
1491MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1492 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1493}
1494
1495MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1496 delete[] mSlots;
1497}
1498
1499void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1500 mSlotCount = slotCount;
1501 mUsingSlotsProtocol = usingSlotsProtocol;
1502
1503 delete[] mSlots;
1504 mSlots = new Slot[slotCount];
1505}
1506
Jeff Brown65fd2512011-08-18 11:20:58 -07001507void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1508 // Unfortunately there is no way to read the initial contents of the slots.
1509 // So when we reset the accumulator, we must assume they are all zeroes.
1510 if (mUsingSlotsProtocol) {
1511 // Query the driver for the current slot index and use it as the initial slot
1512 // before we start reading events from the device. It is possible that the
1513 // current slot index will not be the same as it was when the first event was
1514 // written into the evdev buffer, which means the input mapper could start
1515 // out of sync with the initial state of the events in the evdev buffer.
1516 // In the extremely unlikely case that this happens, the data from
1517 // two slots will be confused until the next ABS_MT_SLOT event is received.
1518 // This can cause the touch point to "jump", but at least there will be
1519 // no stuck touches.
1520 int32_t initialSlot;
1521 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1522 ABS_MT_SLOT, &initialSlot);
1523 if (status) {
1524 LOGD("Could not retrieve current multitouch slot index. status=%d", status);
1525 initialSlot = -1;
1526 }
1527 clearSlots(initialSlot);
1528 } else {
1529 clearSlots(-1);
1530 }
1531}
1532
Jeff Brown49754db2011-07-01 17:37:58 -07001533void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001534 if (mSlots) {
1535 for (size_t i = 0; i < mSlotCount; i++) {
1536 mSlots[i].clear();
1537 }
Jeff Brown49754db2011-07-01 17:37:58 -07001538 }
1539 mCurrentSlot = initialSlot;
1540}
1541
1542void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1543 if (rawEvent->type == EV_ABS) {
1544 bool newSlot = false;
1545 if (mUsingSlotsProtocol) {
1546 if (rawEvent->scanCode == ABS_MT_SLOT) {
1547 mCurrentSlot = rawEvent->value;
1548 newSlot = true;
1549 }
1550 } else if (mCurrentSlot < 0) {
1551 mCurrentSlot = 0;
1552 }
1553
1554 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1555#if DEBUG_POINTERS
1556 if (newSlot) {
1557 LOGW("MultiTouch device emitted invalid slot index %d but it "
1558 "should be between 0 and %d; ignoring this slot.",
1559 mCurrentSlot, mSlotCount - 1);
1560 }
1561#endif
1562 } else {
1563 Slot* slot = &mSlots[mCurrentSlot];
1564
1565 switch (rawEvent->scanCode) {
1566 case ABS_MT_POSITION_X:
1567 slot->mInUse = true;
1568 slot->mAbsMTPositionX = rawEvent->value;
1569 break;
1570 case ABS_MT_POSITION_Y:
1571 slot->mInUse = true;
1572 slot->mAbsMTPositionY = rawEvent->value;
1573 break;
1574 case ABS_MT_TOUCH_MAJOR:
1575 slot->mInUse = true;
1576 slot->mAbsMTTouchMajor = rawEvent->value;
1577 break;
1578 case ABS_MT_TOUCH_MINOR:
1579 slot->mInUse = true;
1580 slot->mAbsMTTouchMinor = rawEvent->value;
1581 slot->mHaveAbsMTTouchMinor = true;
1582 break;
1583 case ABS_MT_WIDTH_MAJOR:
1584 slot->mInUse = true;
1585 slot->mAbsMTWidthMajor = rawEvent->value;
1586 break;
1587 case ABS_MT_WIDTH_MINOR:
1588 slot->mInUse = true;
1589 slot->mAbsMTWidthMinor = rawEvent->value;
1590 slot->mHaveAbsMTWidthMinor = true;
1591 break;
1592 case ABS_MT_ORIENTATION:
1593 slot->mInUse = true;
1594 slot->mAbsMTOrientation = rawEvent->value;
1595 break;
1596 case ABS_MT_TRACKING_ID:
1597 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001598 // The slot is no longer in use but it retains its previous contents,
1599 // which may be reused for subsequent touches.
1600 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001601 } else {
1602 slot->mInUse = true;
1603 slot->mAbsMTTrackingId = rawEvent->value;
1604 }
1605 break;
1606 case ABS_MT_PRESSURE:
1607 slot->mInUse = true;
1608 slot->mAbsMTPressure = rawEvent->value;
1609 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001610 case ABS_MT_DISTANCE:
1611 slot->mInUse = true;
1612 slot->mAbsMTDistance = rawEvent->value;
1613 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001614 case ABS_MT_TOOL_TYPE:
1615 slot->mInUse = true;
1616 slot->mAbsMTToolType = rawEvent->value;
1617 slot->mHaveAbsMTToolType = true;
1618 break;
1619 }
1620 }
1621 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1622 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1623 mCurrentSlot += 1;
1624 }
1625}
1626
Jeff Brown65fd2512011-08-18 11:20:58 -07001627void MultiTouchMotionAccumulator::finishSync() {
1628 if (!mUsingSlotsProtocol) {
1629 clearSlots(-1);
1630 }
1631}
1632
Jeff Brown49754db2011-07-01 17:37:58 -07001633
1634// --- MultiTouchMotionAccumulator::Slot ---
1635
1636MultiTouchMotionAccumulator::Slot::Slot() {
1637 clear();
1638}
1639
Jeff Brown49754db2011-07-01 17:37:58 -07001640void MultiTouchMotionAccumulator::Slot::clear() {
1641 mInUse = false;
1642 mHaveAbsMTTouchMinor = false;
1643 mHaveAbsMTWidthMinor = false;
1644 mHaveAbsMTToolType = false;
1645 mAbsMTPositionX = 0;
1646 mAbsMTPositionY = 0;
1647 mAbsMTTouchMajor = 0;
1648 mAbsMTTouchMinor = 0;
1649 mAbsMTWidthMajor = 0;
1650 mAbsMTWidthMinor = 0;
1651 mAbsMTOrientation = 0;
1652 mAbsMTTrackingId = -1;
1653 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001654 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001655 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001656}
1657
1658int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1659 if (mHaveAbsMTToolType) {
1660 switch (mAbsMTToolType) {
1661 case MT_TOOL_FINGER:
1662 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1663 case MT_TOOL_PEN:
1664 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1665 }
1666 }
1667 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1668}
1669
1670
Jeff Brown6d0fec22010-07-23 21:28:06 -07001671// --- InputMapper ---
1672
1673InputMapper::InputMapper(InputDevice* device) :
1674 mDevice(device), mContext(device->getContext()) {
1675}
1676
1677InputMapper::~InputMapper() {
1678}
1679
1680void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1681 info->addSource(getSources());
1682}
1683
Jeff Brownef3d7e82010-09-30 14:33:04 -07001684void InputMapper::dump(String8& dump) {
1685}
1686
Jeff Brown65fd2512011-08-18 11:20:58 -07001687void InputMapper::configure(nsecs_t when,
1688 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001689}
1690
Jeff Brown65fd2512011-08-18 11:20:58 -07001691void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001692}
1693
Jeff Brownaa3855d2011-03-17 01:34:19 -07001694void InputMapper::timeoutExpired(nsecs_t when) {
1695}
1696
Jeff Brown6d0fec22010-07-23 21:28:06 -07001697int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1698 return AKEY_STATE_UNKNOWN;
1699}
1700
1701int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1702 return AKEY_STATE_UNKNOWN;
1703}
1704
1705int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1706 return AKEY_STATE_UNKNOWN;
1707}
1708
1709bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1710 const int32_t* keyCodes, uint8_t* outFlags) {
1711 return false;
1712}
1713
1714int32_t InputMapper::getMetaState() {
1715 return 0;
1716}
1717
Jeff Brown05dc66a2011-03-02 14:41:58 -08001718void InputMapper::fadePointer() {
1719}
1720
Jeff Brownbe1aa822011-07-27 16:04:54 -07001721status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1722 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1723}
1724
Jeff Browncb1404e2011-01-15 18:14:15 -08001725void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1726 const RawAbsoluteAxisInfo& axis, const char* name) {
1727 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001728 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1729 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001730 } else {
1731 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1732 }
1733}
1734
Jeff Brown6d0fec22010-07-23 21:28:06 -07001735
1736// --- SwitchInputMapper ---
1737
1738SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1739 InputMapper(device) {
1740}
1741
1742SwitchInputMapper::~SwitchInputMapper() {
1743}
1744
1745uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001746 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001747}
1748
1749void SwitchInputMapper::process(const RawEvent* rawEvent) {
1750 switch (rawEvent->type) {
1751 case EV_SW:
1752 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1753 break;
1754 }
1755}
1756
1757void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001758 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1759 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001760}
1761
1762int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1763 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1764}
1765
1766
1767// --- KeyboardInputMapper ---
1768
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001769KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001770 uint32_t source, int32_t keyboardType) :
1771 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001772 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001773}
1774
1775KeyboardInputMapper::~KeyboardInputMapper() {
1776}
1777
Jeff Brown6d0fec22010-07-23 21:28:06 -07001778uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001779 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001780}
1781
1782void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1783 InputMapper::populateDeviceInfo(info);
1784
1785 info->setKeyboardType(mKeyboardType);
1786}
1787
Jeff Brownef3d7e82010-09-30 14:33:04 -07001788void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001789 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1790 dumpParameters(dump);
1791 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001792 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001793 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1794 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1795 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001796}
1797
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001798
Jeff Brown65fd2512011-08-18 11:20:58 -07001799void KeyboardInputMapper::configure(nsecs_t when,
1800 const InputReaderConfiguration* config, uint32_t changes) {
1801 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001802
Jeff Brown474dcb52011-06-14 20:22:50 -07001803 if (!changes) { // first time only
1804 // Configure basic parameters.
1805 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07001806 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001807
Jeff Brown65fd2512011-08-18 11:20:58 -07001808 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1809 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1810 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
1811 false /*external*/, NULL, NULL, &mOrientation)) {
1812 mOrientation = DISPLAY_ORIENTATION_0;
1813 }
1814 } else {
1815 mOrientation = DISPLAY_ORIENTATION_0;
1816 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001817 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001818}
1819
1820void KeyboardInputMapper::configureParameters() {
1821 mParameters.orientationAware = false;
1822 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1823 mParameters.orientationAware);
1824
Jeff Brownbc68a592011-07-25 12:58:12 -07001825 mParameters.associatedDisplayId = -1;
1826 if (mParameters.orientationAware) {
1827 mParameters.associatedDisplayId = 0;
1828 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001829}
1830
1831void KeyboardInputMapper::dumpParameters(String8& dump) {
1832 dump.append(INDENT3 "Parameters:\n");
1833 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1834 mParameters.associatedDisplayId);
1835 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1836 toString(mParameters.orientationAware));
1837}
1838
Jeff Brown65fd2512011-08-18 11:20:58 -07001839void KeyboardInputMapper::reset(nsecs_t when) {
1840 mMetaState = AMETA_NONE;
1841 mDownTime = 0;
1842 mKeyDowns.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001843
Jeff Brownbe1aa822011-07-27 16:04:54 -07001844 resetLedState();
1845
Jeff Brown65fd2512011-08-18 11:20:58 -07001846 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001847}
1848
1849void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1850 switch (rawEvent->type) {
1851 case EV_KEY: {
1852 int32_t scanCode = rawEvent->scanCode;
1853 if (isKeyboardOrGamepadKey(scanCode)) {
1854 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1855 rawEvent->flags);
1856 }
1857 break;
1858 }
1859 }
1860}
1861
1862bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1863 return scanCode < BTN_MOUSE
1864 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001865 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001866 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001867}
1868
Jeff Brown6328cdc2010-07-29 18:18:33 -07001869void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1870 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001871
Jeff Brownbe1aa822011-07-27 16:04:54 -07001872 if (down) {
1873 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001874 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001875 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001876 }
Jeff Brownfe508922011-01-18 15:10:10 -08001877
Jeff Brownbe1aa822011-07-27 16:04:54 -07001878 // Add key down.
1879 ssize_t keyDownIndex = findKeyDown(scanCode);
1880 if (keyDownIndex >= 0) {
1881 // key repeat, be sure to use same keycode as before in case of rotation
1882 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001883 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001884 // key down
1885 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1886 && mContext->shouldDropVirtualKey(when,
1887 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001888 return;
1889 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001890
1891 mKeyDowns.push();
1892 KeyDown& keyDown = mKeyDowns.editTop();
1893 keyDown.keyCode = keyCode;
1894 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001895 }
1896
Jeff Brownbe1aa822011-07-27 16:04:54 -07001897 mDownTime = when;
1898 } else {
1899 // Remove key down.
1900 ssize_t keyDownIndex = findKeyDown(scanCode);
1901 if (keyDownIndex >= 0) {
1902 // key up, be sure to use same keycode as before in case of rotation
1903 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1904 mKeyDowns.removeAt(size_t(keyDownIndex));
1905 } else {
1906 // key was not actually down
1907 LOGI("Dropping key up from device %s because the key was not down. "
1908 "keyCode=%d, scanCode=%d",
1909 getDeviceName().string(), keyCode, scanCode);
1910 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001911 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001912 }
Jeff Brownfd035822010-06-30 16:10:35 -07001913
Jeff Brownbe1aa822011-07-27 16:04:54 -07001914 bool metaStateChanged = false;
1915 int32_t oldMetaState = mMetaState;
1916 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1917 if (oldMetaState != newMetaState) {
1918 mMetaState = newMetaState;
1919 metaStateChanged = true;
1920 updateLedState(false);
1921 }
1922
1923 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001924
Jeff Brown56194eb2011-03-02 19:23:13 -08001925 // Key down on external an keyboard should wake the device.
1926 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1927 // For internal keyboards, the key layout file should specify the policy flags for
1928 // each wake key individually.
1929 // TODO: Use the input device configuration to control this behavior more finely.
1930 if (down && getDevice()->isExternal()
1931 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1932 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1933 }
1934
Jeff Brown6328cdc2010-07-29 18:18:33 -07001935 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001936 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001937 }
1938
Jeff Brown05dc66a2011-03-02 14:41:58 -08001939 if (down && !isMetaKey(keyCode)) {
1940 getContext()->fadePointer();
1941 }
1942
Jeff Brownbe1aa822011-07-27 16:04:54 -07001943 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001944 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1945 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001946 getListener()->notifyKey(&args);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001947}
1948
Jeff Brownbe1aa822011-07-27 16:04:54 -07001949ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1950 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001951 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001952 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001953 return i;
1954 }
1955 }
1956 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001957}
1958
Jeff Brown6d0fec22010-07-23 21:28:06 -07001959int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1960 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1961}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001962
Jeff Brown6d0fec22010-07-23 21:28:06 -07001963int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1964 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1965}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001966
Jeff Brown6d0fec22010-07-23 21:28:06 -07001967bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1968 const int32_t* keyCodes, uint8_t* outFlags) {
1969 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1970}
1971
1972int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001973 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001974}
1975
Jeff Brownbe1aa822011-07-27 16:04:54 -07001976void KeyboardInputMapper::resetLedState() {
1977 initializeLedState(mCapsLockLedState, LED_CAPSL);
1978 initializeLedState(mNumLockLedState, LED_NUML);
1979 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001980
Jeff Brownbe1aa822011-07-27 16:04:54 -07001981 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001982}
1983
Jeff Brownbe1aa822011-07-27 16:04:54 -07001984void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001985 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1986 ledState.on = false;
1987}
1988
Jeff Brownbe1aa822011-07-27 16:04:54 -07001989void KeyboardInputMapper::updateLedState(bool reset) {
1990 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001991 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001992 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001993 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001994 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001995 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001996}
1997
Jeff Brownbe1aa822011-07-27 16:04:54 -07001998void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07001999 int32_t led, int32_t modifier, bool reset) {
2000 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002001 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002002 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002003 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2004 ledState.on = desiredState;
2005 }
2006 }
2007}
2008
Jeff Brown6d0fec22010-07-23 21:28:06 -07002009
Jeff Brown83c09682010-12-23 17:50:18 -08002010// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002011
Jeff Brown83c09682010-12-23 17:50:18 -08002012CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002013 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002014}
2015
Jeff Brown83c09682010-12-23 17:50:18 -08002016CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002017}
2018
Jeff Brown83c09682010-12-23 17:50:18 -08002019uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002020 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002021}
2022
Jeff Brown83c09682010-12-23 17:50:18 -08002023void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002024 InputMapper::populateDeviceInfo(info);
2025
Jeff Brown83c09682010-12-23 17:50:18 -08002026 if (mParameters.mode == Parameters::MODE_POINTER) {
2027 float minX, minY, maxX, maxY;
2028 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002029 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2030 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002031 }
2032 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002033 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2034 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002035 }
Jeff Brownefd32662011-03-08 15:13:06 -08002036 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002037
Jeff Brown65fd2512011-08-18 11:20:58 -07002038 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002039 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002040 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002041 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002042 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002043 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002044}
2045
Jeff Brown83c09682010-12-23 17:50:18 -08002046void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002047 dump.append(INDENT2 "Cursor Input Mapper:\n");
2048 dumpParameters(dump);
2049 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2050 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2051 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2052 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2053 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002054 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002055 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002056 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002057 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2058 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002059 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002060 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2061 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2062 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002063}
2064
Jeff Brown65fd2512011-08-18 11:20:58 -07002065void CursorInputMapper::configure(nsecs_t when,
2066 const InputReaderConfiguration* config, uint32_t changes) {
2067 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002068
Jeff Brown474dcb52011-06-14 20:22:50 -07002069 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002070 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002071
Jeff Brown474dcb52011-06-14 20:22:50 -07002072 // Configure basic parameters.
2073 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002074
Jeff Brown474dcb52011-06-14 20:22:50 -07002075 // Configure device mode.
2076 switch (mParameters.mode) {
2077 case Parameters::MODE_POINTER:
2078 mSource = AINPUT_SOURCE_MOUSE;
2079 mXPrecision = 1.0f;
2080 mYPrecision = 1.0f;
2081 mXScale = 1.0f;
2082 mYScale = 1.0f;
2083 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2084 break;
2085 case Parameters::MODE_NAVIGATION:
2086 mSource = AINPUT_SOURCE_TRACKBALL;
2087 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2088 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2089 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2090 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2091 break;
2092 }
2093
2094 mVWheelScale = 1.0f;
2095 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002096 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002097
Jeff Brown474dcb52011-06-14 20:22:50 -07002098 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2099 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2100 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2101 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2102 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002103
2104 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2105 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2106 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2107 false /*external*/, NULL, NULL, &mOrientation)) {
2108 mOrientation = DISPLAY_ORIENTATION_0;
2109 }
2110 } else {
2111 mOrientation = DISPLAY_ORIENTATION_0;
2112 }
2113 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002114}
2115
Jeff Brown83c09682010-12-23 17:50:18 -08002116void CursorInputMapper::configureParameters() {
2117 mParameters.mode = Parameters::MODE_POINTER;
2118 String8 cursorModeString;
2119 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2120 if (cursorModeString == "navigation") {
2121 mParameters.mode = Parameters::MODE_NAVIGATION;
2122 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
2123 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
2124 }
2125 }
2126
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002127 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002128 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002129 mParameters.orientationAware);
2130
Jeff Brownbc68a592011-07-25 12:58:12 -07002131 mParameters.associatedDisplayId = -1;
2132 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2133 mParameters.associatedDisplayId = 0;
2134 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002135}
2136
Jeff Brown83c09682010-12-23 17:50:18 -08002137void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002138 dump.append(INDENT3 "Parameters:\n");
2139 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2140 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002141
2142 switch (mParameters.mode) {
2143 case Parameters::MODE_POINTER:
2144 dump.append(INDENT4 "Mode: pointer\n");
2145 break;
2146 case Parameters::MODE_NAVIGATION:
2147 dump.append(INDENT4 "Mode: navigation\n");
2148 break;
2149 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002150 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002151 }
2152
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002153 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2154 toString(mParameters.orientationAware));
2155}
2156
Jeff Brown65fd2512011-08-18 11:20:58 -07002157void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002158 mButtonState = 0;
2159 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002160
Jeff Brownbe1aa822011-07-27 16:04:54 -07002161 mPointerVelocityControl.reset();
2162 mWheelXVelocityControl.reset();
2163 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002164
Jeff Brown65fd2512011-08-18 11:20:58 -07002165 mCursorButtonAccumulator.reset(getDevice());
2166 mCursorMotionAccumulator.reset(getDevice());
2167 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002168
Jeff Brown65fd2512011-08-18 11:20:58 -07002169 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002170}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002171
Jeff Brown83c09682010-12-23 17:50:18 -08002172void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002173 mCursorButtonAccumulator.process(rawEvent);
2174 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002175 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002176
Jeff Brown49754db2011-07-01 17:37:58 -07002177 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
2178 sync(rawEvent->when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002179 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002180}
2181
Jeff Brown83c09682010-12-23 17:50:18 -08002182void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002183 int32_t lastButtonState = mButtonState;
2184 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2185 mButtonState = currentButtonState;
2186
2187 bool wasDown = isPointerDown(lastButtonState);
2188 bool down = isPointerDown(currentButtonState);
2189 bool downChanged;
2190 if (!wasDown && down) {
2191 mDownTime = when;
2192 downChanged = true;
2193 } else if (wasDown && !down) {
2194 downChanged = true;
2195 } else {
2196 downChanged = false;
2197 }
2198 nsecs_t downTime = mDownTime;
2199 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002200 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002201
2202 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2203 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2204 bool moved = deltaX != 0 || deltaY != 0;
2205
Jeff Brown65fd2512011-08-18 11:20:58 -07002206 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002207 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2208 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002209 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002210 }
2211
Jeff Brown65fd2512011-08-18 11:20:58 -07002212 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002213 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002214 pointerProperties.clear();
2215 pointerProperties.id = 0;
2216 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2217
Jeff Brown6328cdc2010-07-29 18:18:33 -07002218 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002219 pointerCoords.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002220
Jeff Brown65fd2512011-08-18 11:20:58 -07002221 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2222 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002223 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002224
Jeff Brownbe1aa822011-07-27 16:04:54 -07002225 mWheelYVelocityControl.move(when, NULL, &vscroll);
2226 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002227
Jeff Brownbe1aa822011-07-27 16:04:54 -07002228 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002229
Jeff Brownbe1aa822011-07-27 16:04:54 -07002230 if (mPointerController != NULL) {
2231 if (moved || scrolled || buttonsChanged) {
2232 mPointerController->setPresentation(
2233 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002234
Jeff Brownbe1aa822011-07-27 16:04:54 -07002235 if (moved) {
2236 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002237 }
2238
Jeff Brownbe1aa822011-07-27 16:04:54 -07002239 if (buttonsChanged) {
2240 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002241 }
Jeff Brownefd32662011-03-08 15:13:06 -08002242
Jeff Brownbe1aa822011-07-27 16:04:54 -07002243 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002244 }
2245
Jeff Brownbe1aa822011-07-27 16:04:54 -07002246 float x, y;
2247 mPointerController->getPosition(&x, &y);
2248 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2249 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2250 } else {
2251 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2252 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2253 }
2254
2255 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002256
Jeff Brown56194eb2011-03-02 19:23:13 -08002257 // Moving an external trackball or mouse should wake the device.
2258 // We don't do this for internal cursor devices to prevent them from waking up
2259 // the device in your pocket.
2260 // TODO: Use the input device configuration to control this behavior more finely.
2261 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002262 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002263 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2264 }
2265
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002266 // Synthesize key down from buttons if needed.
2267 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2268 policyFlags, lastButtonState, currentButtonState);
2269
2270 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002271 if (downChanged || moved || scrolled || buttonsChanged) {
2272 int32_t metaState = mContext->getGlobalMetaState();
2273 int32_t motionEventAction;
2274 if (downChanged) {
2275 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2276 } else if (down || mPointerController == NULL) {
2277 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2278 } else {
2279 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2280 }
Jeff Brownb6997262010-10-08 22:31:17 -07002281
Jeff Brownbe1aa822011-07-27 16:04:54 -07002282 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2283 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002284 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002285 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002286
Jeff Brownbe1aa822011-07-27 16:04:54 -07002287 // Send hover move after UP to tell the application that the mouse is hovering now.
2288 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2289 && mPointerController != NULL) {
2290 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2291 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2292 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2293 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2294 getListener()->notifyMotion(&hoverArgs);
2295 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002296
Jeff Brownbe1aa822011-07-27 16:04:54 -07002297 // Send scroll events.
2298 if (scrolled) {
2299 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2300 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2301
2302 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2303 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2304 AMOTION_EVENT_EDGE_FLAG_NONE,
2305 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2306 getListener()->notifyMotion(&scrollArgs);
2307 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002308 }
Jeff Browna032cc02011-03-07 16:56:21 -08002309
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002310 // Synthesize key up from buttons if needed.
2311 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2312 policyFlags, lastButtonState, currentButtonState);
2313
Jeff Brown65fd2512011-08-18 11:20:58 -07002314 mCursorMotionAccumulator.finishSync();
2315 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002316}
2317
Jeff Brown83c09682010-12-23 17:50:18 -08002318int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002319 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2320 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2321 } else {
2322 return AKEY_STATE_UNKNOWN;
2323 }
2324}
2325
Jeff Brown05dc66a2011-03-02 14:41:58 -08002326void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002327 if (mPointerController != NULL) {
2328 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2329 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002330}
2331
Jeff Brown6d0fec22010-07-23 21:28:06 -07002332
2333// --- TouchInputMapper ---
2334
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002335TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002336 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002337 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002338 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002339}
2340
2341TouchInputMapper::~TouchInputMapper() {
2342}
2343
2344uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002345 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002346}
2347
2348void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2349 InputMapper::populateDeviceInfo(info);
2350
Jeff Brown65fd2512011-08-18 11:20:58 -07002351 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2352 info->addMotionRange(mOrientedRanges.x);
2353 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002354 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002355
Jeff Brown65fd2512011-08-18 11:20:58 -07002356 if (mOrientedRanges.haveSize) {
2357 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002358 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002359
2360 if (mOrientedRanges.haveTouchSize) {
2361 info->addMotionRange(mOrientedRanges.touchMajor);
2362 info->addMotionRange(mOrientedRanges.touchMinor);
2363 }
2364
2365 if (mOrientedRanges.haveToolSize) {
2366 info->addMotionRange(mOrientedRanges.toolMajor);
2367 info->addMotionRange(mOrientedRanges.toolMinor);
2368 }
2369
2370 if (mOrientedRanges.haveOrientation) {
2371 info->addMotionRange(mOrientedRanges.orientation);
2372 }
2373
2374 if (mOrientedRanges.haveDistance) {
2375 info->addMotionRange(mOrientedRanges.distance);
2376 }
2377
2378 if (mOrientedRanges.haveTilt) {
2379 info->addMotionRange(mOrientedRanges.tilt);
2380 }
2381
2382 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2383 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2384 }
2385 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2386 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2387 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002388 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002389}
2390
Jeff Brownef3d7e82010-09-30 14:33:04 -07002391void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002392 dump.append(INDENT2 "Touch Input Mapper:\n");
2393 dumpParameters(dump);
2394 dumpVirtualKeys(dump);
2395 dumpRawPointerAxes(dump);
2396 dumpCalibration(dump);
2397 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002398
Jeff Brownbe1aa822011-07-27 16:04:54 -07002399 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2400 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2401 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2402 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2403 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2404 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002405 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2406 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002407 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002408 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2409 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002410 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2411 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2412 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2413 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2414 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002415
Jeff Brownbe1aa822011-07-27 16:04:54 -07002416 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002417
Jeff Brownbe1aa822011-07-27 16:04:54 -07002418 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2419 mLastRawPointerData.pointerCount);
2420 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2421 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2422 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2423 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002424 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2425 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002426 pointer.id, pointer.x, pointer.y, pointer.pressure,
2427 pointer.touchMajor, pointer.touchMinor,
2428 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002429 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002430 pointer.toolType, toString(pointer.isHovering));
2431 }
2432
2433 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2434 mLastCookedPointerData.pointerCount);
2435 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2436 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2437 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2438 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2439 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002440 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2441 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002442 pointerProperties.id,
2443 pointerCoords.getX(),
2444 pointerCoords.getY(),
2445 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2446 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2447 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2448 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2449 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2450 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002451 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002452 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2453 pointerProperties.toolType,
2454 toString(mLastCookedPointerData.isHovering(i)));
2455 }
2456
Jeff Brown65fd2512011-08-18 11:20:58 -07002457 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002458 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2459 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002460 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002461 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002462 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002463 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002464 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002465 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002466 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002467 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2468 mPointerGestureMaxSwipeWidth);
2469 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002470}
2471
Jeff Brown65fd2512011-08-18 11:20:58 -07002472void TouchInputMapper::configure(nsecs_t when,
2473 const InputReaderConfiguration* config, uint32_t changes) {
2474 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002475
Jeff Brown474dcb52011-06-14 20:22:50 -07002476 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002477
Jeff Brown474dcb52011-06-14 20:22:50 -07002478 if (!changes) { // first time only
2479 // Configure basic parameters.
2480 configureParameters();
2481
Jeff Brown65fd2512011-08-18 11:20:58 -07002482 // Configure common accumulators.
2483 mCursorScrollAccumulator.configure(getDevice());
2484 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002485
2486 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002487 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002488
2489 // Prepare input device calibration.
2490 parseCalibration();
2491 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002492 }
2493
Jeff Brown474dcb52011-06-14 20:22:50 -07002494 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002495 // Update pointer speed.
2496 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2497 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2498 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002499 }
Jeff Brown8d608662010-08-30 03:02:23 -07002500
Jeff Brown65fd2512011-08-18 11:20:58 -07002501 bool resetNeeded = false;
2502 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002503 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2504 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002505 // Configure device sources, surface dimensions, orientation and
2506 // scaling factors.
2507 configureSurface(when, &resetNeeded);
2508 }
2509
2510 if (changes && resetNeeded) {
2511 // Send reset, unless this is the first time the device has been configured,
2512 // in which case the reader will call reset itself after all mappers are ready.
2513 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002514 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002515}
2516
Jeff Brown8d608662010-08-30 03:02:23 -07002517void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002518 // Use the pointer presentation mode for devices that do not support distinct
2519 // multitouch. The spot-based presentation relies on being able to accurately
2520 // locate two or more fingers on the touch pad.
2521 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2522 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002523
Jeff Brown538881e2011-05-25 18:23:38 -07002524 String8 gestureModeString;
2525 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2526 gestureModeString)) {
2527 if (gestureModeString == "pointer") {
2528 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2529 } else if (gestureModeString == "spots") {
2530 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2531 } else if (gestureModeString != "default") {
2532 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2533 }
2534 }
2535
Jeff Browndeffe072011-08-26 18:38:46 -07002536 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2537 // The device is a touch screen.
2538 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2539 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2540 // The device is a pointing device like a track pad.
2541 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2542 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002543 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2544 // The device is a cursor device with a touch pad attached.
2545 // By default don't use the touch pad to move the pointer.
2546 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2547 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002548 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002549 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2550 }
2551
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002552 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002553 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2554 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002555 if (deviceTypeString == "touchScreen") {
2556 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002557 } else if (deviceTypeString == "touchPad") {
2558 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002559 } else if (deviceTypeString == "pointer") {
2560 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002561 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002562 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2563 }
2564 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002565
Jeff Brownefd32662011-03-08 15:13:06 -08002566 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002567 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2568 mParameters.orientationAware);
2569
Jeff Brownbc68a592011-07-25 12:58:12 -07002570 mParameters.associatedDisplayId = -1;
2571 mParameters.associatedDisplayIsExternal = false;
2572 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002573 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002574 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2575 mParameters.associatedDisplayIsExternal =
2576 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2577 && getDevice()->isExternal();
2578 mParameters.associatedDisplayId = 0;
2579 }
Jeff Brown8d608662010-08-30 03:02:23 -07002580}
2581
Jeff Brownef3d7e82010-09-30 14:33:04 -07002582void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002583 dump.append(INDENT3 "Parameters:\n");
2584
Jeff Brown538881e2011-05-25 18:23:38 -07002585 switch (mParameters.gestureMode) {
2586 case Parameters::GESTURE_MODE_POINTER:
2587 dump.append(INDENT4 "GestureMode: pointer\n");
2588 break;
2589 case Parameters::GESTURE_MODE_SPOTS:
2590 dump.append(INDENT4 "GestureMode: spots\n");
2591 break;
2592 default:
2593 assert(false);
2594 }
2595
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002596 switch (mParameters.deviceType) {
2597 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2598 dump.append(INDENT4 "DeviceType: touchScreen\n");
2599 break;
2600 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2601 dump.append(INDENT4 "DeviceType: touchPad\n");
2602 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002603 case Parameters::DEVICE_TYPE_POINTER:
2604 dump.append(INDENT4 "DeviceType: pointer\n");
2605 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002606 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002607 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002608 }
2609
Jeff Brown65fd2512011-08-18 11:20:58 -07002610 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2611 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002612 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2613 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002614}
2615
Jeff Brownbe1aa822011-07-27 16:04:54 -07002616void TouchInputMapper::configureRawPointerAxes() {
2617 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002618}
2619
Jeff Brownbe1aa822011-07-27 16:04:54 -07002620void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2621 dump.append(INDENT3 "Raw Touch Axes:\n");
2622 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2623 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2624 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2625 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2626 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2627 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2628 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2629 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2630 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002631 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2632 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002633 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2634 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002635}
2636
Jeff Brown65fd2512011-08-18 11:20:58 -07002637void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2638 int32_t oldDeviceMode = mDeviceMode;
2639
2640 // Determine device mode.
2641 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2642 && mConfig.pointerGesturesEnabled) {
2643 mSource = AINPUT_SOURCE_MOUSE;
2644 mDeviceMode = DEVICE_MODE_POINTER;
2645 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2646 && mParameters.associatedDisplayId >= 0) {
2647 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2648 mDeviceMode = DEVICE_MODE_DIRECT;
2649 } else {
2650 mSource = AINPUT_SOURCE_TOUCHPAD;
2651 mDeviceMode = DEVICE_MODE_UNSCALED;
2652 }
2653
Jeff Brown9626b142011-03-03 02:09:54 -08002654 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002655 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002656 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2657 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002658 mDeviceMode = DEVICE_MODE_DISABLED;
2659 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002660 }
2661
Jeff Brown65fd2512011-08-18 11:20:58 -07002662 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002663 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002664 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002665 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002666 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2667 &mAssociatedDisplayOrientation)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002668 LOGI(INDENT "Touch device '%s' could not query the properties of its associated "
2669 "display %d. The device will be inoperable until the display size "
2670 "becomes available.",
2671 getDeviceName().string(), mParameters.associatedDisplayId);
2672 mDeviceMode = DEVICE_MODE_DISABLED;
2673 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002674 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002675 }
2676
Jeff Brown65fd2512011-08-18 11:20:58 -07002677 // Configure dimensions.
2678 int32_t width, height, orientation;
2679 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2680 width = mAssociatedDisplayWidth;
2681 height = mAssociatedDisplayHeight;
2682 orientation = mParameters.orientationAware ?
2683 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2684 } else {
2685 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2686 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2687 orientation = DISPLAY_ORIENTATION_0;
2688 }
2689
2690 // If moving between pointer modes, need to reset some state.
2691 bool deviceModeChanged;
2692 if (mDeviceMode != oldDeviceMode) {
2693 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002694 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002695 }
2696
Jeff Browndaf4a122011-08-26 17:14:14 -07002697 // Create pointer controller if needed.
2698 if (mDeviceMode == DEVICE_MODE_POINTER ||
2699 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2700 if (mPointerController == NULL) {
2701 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2702 }
2703 } else {
2704 mPointerController.clear();
2705 }
2706
Jeff Brownbe1aa822011-07-27 16:04:54 -07002707 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002708 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002709 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002710 }
2711
Jeff Brownbe1aa822011-07-27 16:04:54 -07002712 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002713 if (sizeChanged || deviceModeChanged) {
2714 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
2715 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002716
Jeff Brownbe1aa822011-07-27 16:04:54 -07002717 mSurfaceWidth = width;
2718 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002719
Jeff Brown8d608662010-08-30 03:02:23 -07002720 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002721 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2722 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2723 mXPrecision = 1.0f / mXScale;
2724 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002725
Jeff Brownbe1aa822011-07-27 16:04:54 -07002726 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002727 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002728 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002729 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002730
Jeff Brownbe1aa822011-07-27 16:04:54 -07002731 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002732
Jeff Brown8d608662010-08-30 03:02:23 -07002733 // Scale factor for terms that are not oriented in a particular axis.
2734 // If the pixels are square then xScale == yScale otherwise we fake it
2735 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002736 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002737
Jeff Brown8d608662010-08-30 03:02:23 -07002738 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002739 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002740
Jeff Browna1f89ce2011-08-11 00:05:01 -07002741 // Size factors.
2742 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2743 if (mRawPointerAxes.touchMajor.valid
2744 && mRawPointerAxes.touchMajor.maxValue != 0) {
2745 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2746 } else if (mRawPointerAxes.toolMajor.valid
2747 && mRawPointerAxes.toolMajor.maxValue != 0) {
2748 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2749 } else {
2750 mSizeScale = 0.0f;
2751 }
2752
Jeff Brownbe1aa822011-07-27 16:04:54 -07002753 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002754 mOrientedRanges.haveToolSize = true;
2755 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002756
Jeff Brownbe1aa822011-07-27 16:04:54 -07002757 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002758 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002759 mOrientedRanges.touchMajor.min = 0;
2760 mOrientedRanges.touchMajor.max = diagonalSize;
2761 mOrientedRanges.touchMajor.flat = 0;
2762 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002763
Jeff Brownbe1aa822011-07-27 16:04:54 -07002764 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2765 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002766
Jeff Brownbe1aa822011-07-27 16:04:54 -07002767 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002768 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002769 mOrientedRanges.toolMajor.min = 0;
2770 mOrientedRanges.toolMajor.max = diagonalSize;
2771 mOrientedRanges.toolMajor.flat = 0;
2772 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002773
Jeff Brownbe1aa822011-07-27 16:04:54 -07002774 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2775 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002776
2777 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002778 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002779 mOrientedRanges.size.min = 0;
2780 mOrientedRanges.size.max = 1.0;
2781 mOrientedRanges.size.flat = 0;
2782 mOrientedRanges.size.fuzz = 0;
2783 } else {
2784 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002785 }
2786
2787 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002788 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002789 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2790 || mCalibration.pressureCalibration
2791 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2792 if (mCalibration.havePressureScale) {
2793 mPressureScale = mCalibration.pressureScale;
2794 } else if (mRawPointerAxes.pressure.valid
2795 && mRawPointerAxes.pressure.maxValue != 0) {
2796 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002797 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002798 }
Jeff Brown8d608662010-08-30 03:02:23 -07002799
Jeff Brown65fd2512011-08-18 11:20:58 -07002800 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2801 mOrientedRanges.pressure.source = mSource;
2802 mOrientedRanges.pressure.min = 0;
2803 mOrientedRanges.pressure.max = 1.0;
2804 mOrientedRanges.pressure.flat = 0;
2805 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002806
Jeff Brown65fd2512011-08-18 11:20:58 -07002807 // Tilt
2808 mTiltXCenter = 0;
2809 mTiltXScale = 0;
2810 mTiltYCenter = 0;
2811 mTiltYScale = 0;
2812 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2813 if (mHaveTilt) {
2814 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2815 mRawPointerAxes.tiltX.maxValue);
2816 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2817 mRawPointerAxes.tiltY.maxValue);
2818 mTiltXScale = M_PI / 180;
2819 mTiltYScale = M_PI / 180;
2820
2821 mOrientedRanges.haveTilt = true;
2822
2823 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2824 mOrientedRanges.tilt.source = mSource;
2825 mOrientedRanges.tilt.min = 0;
2826 mOrientedRanges.tilt.max = M_PI_2;
2827 mOrientedRanges.tilt.flat = 0;
2828 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002829 }
2830
Jeff Brown8d608662010-08-30 03:02:23 -07002831 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002832 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002833 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002834 if (mHaveTilt) {
2835 mOrientedRanges.haveOrientation = true;
2836
2837 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2838 mOrientedRanges.orientation.source = mSource;
2839 mOrientedRanges.orientation.min = -M_PI;
2840 mOrientedRanges.orientation.max = M_PI;
2841 mOrientedRanges.orientation.flat = 0;
2842 mOrientedRanges.orientation.fuzz = 0;
2843 } else if (mCalibration.orientationCalibration !=
2844 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002845 if (mCalibration.orientationCalibration
2846 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002847 if (mRawPointerAxes.orientation.valid) {
2848 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2849 mRawPointerAxes.orientation.maxValue);
2850 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2851 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002852 }
2853 }
2854
Jeff Brownbe1aa822011-07-27 16:04:54 -07002855 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002856
Jeff Brownbe1aa822011-07-27 16:04:54 -07002857 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002858 mOrientedRanges.orientation.source = mSource;
2859 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002860 mOrientedRanges.orientation.max = M_PI_2;
2861 mOrientedRanges.orientation.flat = 0;
2862 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002863 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002864
2865 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002866 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002867 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2868 if (mCalibration.distanceCalibration
2869 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2870 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002871 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002872 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002873 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002874 }
2875 }
2876
Jeff Brownbe1aa822011-07-27 16:04:54 -07002877 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002878
Jeff Brownbe1aa822011-07-27 16:04:54 -07002879 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002880 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002881 mOrientedRanges.distance.min =
2882 mRawPointerAxes.distance.minValue * mDistanceScale;
2883 mOrientedRanges.distance.max =
2884 mRawPointerAxes.distance.minValue * mDistanceScale;
2885 mOrientedRanges.distance.flat = 0;
2886 mOrientedRanges.distance.fuzz =
2887 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002888 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002889 }
2890
Jeff Brown65fd2512011-08-18 11:20:58 -07002891 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002892 // Compute oriented surface dimensions, precision, scales and ranges.
2893 // Note that the maximum value reported is an inclusive maximum value so it is one
2894 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002895 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002896 case DISPLAY_ORIENTATION_90:
2897 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002898 mOrientedSurfaceWidth = mSurfaceHeight;
2899 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002900
Jeff Brownbe1aa822011-07-27 16:04:54 -07002901 mOrientedXPrecision = mYPrecision;
2902 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002903
Jeff Brownbe1aa822011-07-27 16:04:54 -07002904 mOrientedRanges.x.min = 0;
2905 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2906 * mYScale;
2907 mOrientedRanges.x.flat = 0;
2908 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002909
Jeff Brownbe1aa822011-07-27 16:04:54 -07002910 mOrientedRanges.y.min = 0;
2911 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2912 * mXScale;
2913 mOrientedRanges.y.flat = 0;
2914 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002915 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002916
Jeff Brown6d0fec22010-07-23 21:28:06 -07002917 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002918 mOrientedSurfaceWidth = mSurfaceWidth;
2919 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002920
Jeff Brownbe1aa822011-07-27 16:04:54 -07002921 mOrientedXPrecision = mXPrecision;
2922 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002923
Jeff Brownbe1aa822011-07-27 16:04:54 -07002924 mOrientedRanges.x.min = 0;
2925 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2926 * mXScale;
2927 mOrientedRanges.x.flat = 0;
2928 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002929
Jeff Brownbe1aa822011-07-27 16:04:54 -07002930 mOrientedRanges.y.min = 0;
2931 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2932 * mYScale;
2933 mOrientedRanges.y.flat = 0;
2934 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002935 break;
2936 }
Jeff Brownace13b12011-03-09 17:39:48 -08002937
2938 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002939 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002940 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2941 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002942 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002943 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2944 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002945
Jeff Brown2352b972011-04-12 22:39:53 -07002946 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d42011-06-01 12:33:19 -07002947 // given area relative to the diagonal size of the display when no acceleration
2948 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002949 // Assume that the touch pad has a square aspect ratio such that movements in
2950 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07002951 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002952 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002953 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002954
2955 // Scale zooms to cover a smaller range of the display than movements do.
2956 // This value determines the area around the pointer that is affected by freeform
2957 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07002958 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002959 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002960 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002961
Jeff Brown2352b972011-04-12 22:39:53 -07002962 // Max width between pointers to detect a swipe gesture is more than some fraction
2963 // of the diagonal axis of the touch pad. Touches that are wider than this are
2964 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002965 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002966 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002967 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002968
Jeff Brown65fd2512011-08-18 11:20:58 -07002969 // Abort current pointer usages because the state has changed.
2970 abortPointerUsage(when, 0 /*policyFlags*/);
2971
2972 // Inform the dispatcher about the changes.
2973 *outResetNeeded = true;
2974 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002975}
2976
Jeff Brownbe1aa822011-07-27 16:04:54 -07002977void TouchInputMapper::dumpSurface(String8& dump) {
2978 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2979 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2980 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002981}
2982
Jeff Brownbe1aa822011-07-27 16:04:54 -07002983void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002984 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002985 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002986
Jeff Brownbe1aa822011-07-27 16:04:54 -07002987 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002988
Jeff Brown6328cdc2010-07-29 18:18:33 -07002989 if (virtualKeyDefinitions.size() == 0) {
2990 return;
2991 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002992
Jeff Brownbe1aa822011-07-27 16:04:54 -07002993 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002994
Jeff Brownbe1aa822011-07-27 16:04:54 -07002995 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2996 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2997 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2998 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002999
3000 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003001 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003002 virtualKeyDefinitions[i];
3003
Jeff Brownbe1aa822011-07-27 16:04:54 -07003004 mVirtualKeys.add();
3005 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003006
3007 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3008 int32_t keyCode;
3009 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08003010 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07003011 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003012 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
3013 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003014 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003015 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003016 }
3017
Jeff Brown6328cdc2010-07-29 18:18:33 -07003018 virtualKey.keyCode = keyCode;
3019 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003020
Jeff Brown6328cdc2010-07-29 18:18:33 -07003021 // convert the key definition's display coordinates into touch coordinates for a hit box
3022 int32_t halfWidth = virtualKeyDefinition.width / 2;
3023 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003024
Jeff Brown6328cdc2010-07-29 18:18:33 -07003025 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003026 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003027 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003028 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003029 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003030 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003031 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003032 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003033 }
3034}
3035
Jeff Brownbe1aa822011-07-27 16:04:54 -07003036void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3037 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003038 dump.append(INDENT3 "Virtual Keys:\n");
3039
Jeff Brownbe1aa822011-07-27 16:04:54 -07003040 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3041 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003042 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3043 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3044 i, virtualKey.scanCode, virtualKey.keyCode,
3045 virtualKey.hitLeft, virtualKey.hitRight,
3046 virtualKey.hitTop, virtualKey.hitBottom);
3047 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003048 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003049}
3050
Jeff Brown8d608662010-08-30 03:02:23 -07003051void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003052 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003053 Calibration& out = mCalibration;
3054
Jeff Browna1f89ce2011-08-11 00:05:01 -07003055 // Size
3056 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3057 String8 sizeCalibrationString;
3058 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3059 if (sizeCalibrationString == "none") {
3060 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3061 } else if (sizeCalibrationString == "geometric") {
3062 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3063 } else if (sizeCalibrationString == "diameter") {
3064 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3065 } else if (sizeCalibrationString == "area") {
3066 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3067 } else if (sizeCalibrationString != "default") {
3068 LOGW("Invalid value for touch.size.calibration: '%s'",
3069 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003070 }
3071 }
3072
Jeff Browna1f89ce2011-08-11 00:05:01 -07003073 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3074 out.sizeScale);
3075 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3076 out.sizeBias);
3077 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3078 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003079
3080 // Pressure
3081 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3082 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003083 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003084 if (pressureCalibrationString == "none") {
3085 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3086 } else if (pressureCalibrationString == "physical") {
3087 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3088 } else if (pressureCalibrationString == "amplitude") {
3089 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3090 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003091 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003092 pressureCalibrationString.string());
3093 }
3094 }
3095
Jeff Brown8d608662010-08-30 03:02:23 -07003096 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3097 out.pressureScale);
3098
Jeff Brown8d608662010-08-30 03:02:23 -07003099 // Orientation
3100 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3101 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003102 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003103 if (orientationCalibrationString == "none") {
3104 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3105 } else if (orientationCalibrationString == "interpolated") {
3106 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003107 } else if (orientationCalibrationString == "vector") {
3108 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003109 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003110 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003111 orientationCalibrationString.string());
3112 }
3113 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003114
3115 // Distance
3116 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3117 String8 distanceCalibrationString;
3118 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3119 if (distanceCalibrationString == "none") {
3120 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3121 } else if (distanceCalibrationString == "scaled") {
3122 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3123 } else if (distanceCalibrationString != "default") {
3124 LOGW("Invalid value for touch.distance.calibration: '%s'",
3125 distanceCalibrationString.string());
3126 }
3127 }
3128
3129 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3130 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003131}
3132
3133void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003134 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003135 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3136 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3137 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003138 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003139 } else {
3140 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3141 }
Jeff Brown8d608662010-08-30 03:02:23 -07003142
Jeff Browna1f89ce2011-08-11 00:05:01 -07003143 // Pressure
3144 if (mRawPointerAxes.pressure.valid) {
3145 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3146 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3147 }
3148 } else {
3149 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003150 }
3151
3152 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003153 if (mRawPointerAxes.orientation.valid) {
3154 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003155 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003156 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003157 } else {
3158 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003159 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003160
3161 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003162 if (mRawPointerAxes.distance.valid) {
3163 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003164 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003165 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003166 } else {
3167 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003168 }
Jeff Brown8d608662010-08-30 03:02:23 -07003169}
3170
Jeff Brownef3d7e82010-09-30 14:33:04 -07003171void TouchInputMapper::dumpCalibration(String8& dump) {
3172 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003173
Jeff Browna1f89ce2011-08-11 00:05:01 -07003174 // Size
3175 switch (mCalibration.sizeCalibration) {
3176 case Calibration::SIZE_CALIBRATION_NONE:
3177 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003178 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003179 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3180 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003181 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003182 case Calibration::SIZE_CALIBRATION_DIAMETER:
3183 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3184 break;
3185 case Calibration::SIZE_CALIBRATION_AREA:
3186 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003187 break;
3188 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003189 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003190 }
3191
Jeff Browna1f89ce2011-08-11 00:05:01 -07003192 if (mCalibration.haveSizeScale) {
3193 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3194 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003195 }
3196
Jeff Browna1f89ce2011-08-11 00:05:01 -07003197 if (mCalibration.haveSizeBias) {
3198 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3199 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003200 }
3201
Jeff Browna1f89ce2011-08-11 00:05:01 -07003202 if (mCalibration.haveSizeIsSummed) {
3203 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3204 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003205 }
3206
3207 // Pressure
3208 switch (mCalibration.pressureCalibration) {
3209 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003210 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003211 break;
3212 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003213 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003214 break;
3215 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003216 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003217 break;
3218 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003219 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003220 }
3221
Jeff Brown8d608662010-08-30 03:02:23 -07003222 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003223 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3224 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003225 }
3226
Jeff Brown8d608662010-08-30 03:02:23 -07003227 // Orientation
3228 switch (mCalibration.orientationCalibration) {
3229 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003230 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003231 break;
3232 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003233 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003234 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003235 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3236 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3237 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003238 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003239 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003240 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003241
3242 // Distance
3243 switch (mCalibration.distanceCalibration) {
3244 case Calibration::DISTANCE_CALIBRATION_NONE:
3245 dump.append(INDENT4 "touch.distance.calibration: none\n");
3246 break;
3247 case Calibration::DISTANCE_CALIBRATION_SCALED:
3248 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3249 break;
3250 default:
3251 LOG_ASSERT(false);
3252 }
3253
3254 if (mCalibration.haveDistanceScale) {
3255 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3256 mCalibration.distanceScale);
3257 }
Jeff Brown8d608662010-08-30 03:02:23 -07003258}
3259
Jeff Brown65fd2512011-08-18 11:20:58 -07003260void TouchInputMapper::reset(nsecs_t when) {
3261 mCursorButtonAccumulator.reset(getDevice());
3262 mCursorScrollAccumulator.reset(getDevice());
3263 mTouchButtonAccumulator.reset(getDevice());
3264
3265 mPointerVelocityControl.reset();
3266 mWheelXVelocityControl.reset();
3267 mWheelYVelocityControl.reset();
3268
Jeff Brownbe1aa822011-07-27 16:04:54 -07003269 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003270 mLastRawPointerData.clear();
3271 mCurrentCookedPointerData.clear();
3272 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003273 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003274 mLastButtonState = 0;
3275 mCurrentRawVScroll = 0;
3276 mCurrentRawHScroll = 0;
3277 mCurrentFingerIdBits.clear();
3278 mLastFingerIdBits.clear();
3279 mCurrentStylusIdBits.clear();
3280 mLastStylusIdBits.clear();
3281 mCurrentMouseIdBits.clear();
3282 mLastMouseIdBits.clear();
3283 mPointerUsage = POINTER_USAGE_NONE;
3284 mSentHoverEnter = false;
3285 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003286
Jeff Brown65fd2512011-08-18 11:20:58 -07003287 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003288
Jeff Brown65fd2512011-08-18 11:20:58 -07003289 mPointerGesture.reset();
3290 mPointerSimple.reset();
3291
3292 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003293 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3294 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003295 }
3296
Jeff Brown65fd2512011-08-18 11:20:58 -07003297 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003298}
3299
Jeff Brown65fd2512011-08-18 11:20:58 -07003300void TouchInputMapper::process(const RawEvent* rawEvent) {
3301 mCursorButtonAccumulator.process(rawEvent);
3302 mCursorScrollAccumulator.process(rawEvent);
3303 mTouchButtonAccumulator.process(rawEvent);
3304
3305 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
3306 sync(rawEvent->when);
3307 }
3308}
3309
3310void TouchInputMapper::sync(nsecs_t when) {
3311 // Sync button state.
3312 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3313 | mCursorButtonAccumulator.getButtonState();
3314
3315 // Sync scroll state.
3316 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3317 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3318 mCursorScrollAccumulator.finishSync();
3319
3320 // Sync touch state.
3321 bool havePointerIds = true;
3322 mCurrentRawPointerData.clear();
3323 syncTouch(when, &havePointerIds);
3324
Jeff Brownaa3855d2011-03-17 01:34:19 -07003325#if DEBUG_RAW_EVENTS
3326 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003327 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3328 mLastRawPointerData.pointerCount,
3329 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003330 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003331 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3332 "hovering ids 0x%08x -> 0x%08x",
3333 mLastRawPointerData.pointerCount,
3334 mCurrentRawPointerData.pointerCount,
3335 mLastRawPointerData.touchingIdBits.value,
3336 mCurrentRawPointerData.touchingIdBits.value,
3337 mLastRawPointerData.hoveringIdBits.value,
3338 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003339 }
3340#endif
3341
Jeff Brown65fd2512011-08-18 11:20:58 -07003342 // Reset state that we will compute below.
3343 mCurrentFingerIdBits.clear();
3344 mCurrentStylusIdBits.clear();
3345 mCurrentMouseIdBits.clear();
3346 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003347
Jeff Brown65fd2512011-08-18 11:20:58 -07003348 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3349 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003350 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003351 mCurrentButtonState = 0;
3352 } else {
3353 // Preprocess pointer data.
3354 if (!havePointerIds) {
3355 assignPointerIds();
3356 }
3357
3358 // Handle policy on initial down or hover events.
3359 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003360 bool initialDown = mLastRawPointerData.pointerCount == 0
3361 && mCurrentRawPointerData.pointerCount != 0;
3362 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3363 if (initialDown || buttonsPressed) {
3364 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003365 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003366 getContext()->fadePointer();
3367 }
3368
3369 // Initial downs on external touch devices should wake the device.
3370 // We don't do this for internal touch screens to prevent them from waking
3371 // up in your pocket.
3372 // TODO: Use the input device configuration to control this behavior more finely.
3373 if (getDevice()->isExternal()) {
3374 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3375 }
3376 }
3377
3378 // Synthesize key down from raw buttons if needed.
3379 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3380 policyFlags, mLastButtonState, mCurrentButtonState);
3381
3382 // Consume raw off-screen touches before cooking pointer data.
3383 // If touches are consumed, subsequent code will not receive any pointer data.
3384 if (consumeRawTouches(when, policyFlags)) {
3385 mCurrentRawPointerData.clear();
3386 }
3387
3388 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3389 // with cooked pointer data that has the same ids and indices as the raw data.
3390 // The following code can use either the raw or cooked data, as needed.
3391 cookPointerData();
3392
3393 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003394 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003395 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3396 uint32_t id = idBits.clearFirstMarkedBit();
3397 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3398 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3399 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3400 mCurrentStylusIdBits.markBit(id);
3401 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3402 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3403 mCurrentFingerIdBits.markBit(id);
3404 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3405 mCurrentMouseIdBits.markBit(id);
3406 }
3407 }
3408 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3409 uint32_t id = idBits.clearFirstMarkedBit();
3410 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3411 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3412 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3413 mCurrentStylusIdBits.markBit(id);
3414 }
3415 }
3416
3417 // Stylus takes precedence over all tools, then mouse, then finger.
3418 PointerUsage pointerUsage = mPointerUsage;
3419 if (!mCurrentStylusIdBits.isEmpty()) {
3420 mCurrentMouseIdBits.clear();
3421 mCurrentFingerIdBits.clear();
3422 pointerUsage = POINTER_USAGE_STYLUS;
3423 } else if (!mCurrentMouseIdBits.isEmpty()) {
3424 mCurrentFingerIdBits.clear();
3425 pointerUsage = POINTER_USAGE_MOUSE;
3426 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3427 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003428 }
3429
3430 dispatchPointerUsage(when, policyFlags, pointerUsage);
3431 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003432 if (mDeviceMode == DEVICE_MODE_DIRECT
3433 && mConfig.showTouches && mPointerController != NULL) {
3434 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3435 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3436
3437 mPointerController->setButtonState(mCurrentButtonState);
3438 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3439 mCurrentCookedPointerData.idToIndex,
3440 mCurrentCookedPointerData.touchingIdBits);
3441 }
3442
Jeff Brown65fd2512011-08-18 11:20:58 -07003443 dispatchHoverExit(when, policyFlags);
3444 dispatchTouches(when, policyFlags);
3445 dispatchHoverEnterAndMove(when, policyFlags);
3446 }
3447
3448 // Synthesize key up from raw buttons if needed.
3449 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3450 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003451 }
3452
Jeff Brown6328cdc2010-07-29 18:18:33 -07003453 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003454 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3455 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3456 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003457 mLastFingerIdBits = mCurrentFingerIdBits;
3458 mLastStylusIdBits = mCurrentStylusIdBits;
3459 mLastMouseIdBits = mCurrentMouseIdBits;
3460
3461 // Clear some transient state.
3462 mCurrentRawVScroll = 0;
3463 mCurrentRawHScroll = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003464}
3465
Jeff Brown79ac9692011-04-19 21:20:10 -07003466void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003467 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003468 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3469 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3470 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003471 }
3472}
3473
Jeff Brownbe1aa822011-07-27 16:04:54 -07003474bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3475 // Check for release of a virtual key.
3476 if (mCurrentVirtualKey.down) {
3477 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3478 // Pointer went up while virtual key was down.
3479 mCurrentVirtualKey.down = false;
3480 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003481#if DEBUG_VIRTUAL_KEYS
3482 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003483 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003484#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003485 dispatchVirtualKey(when, policyFlags,
3486 AKEY_EVENT_ACTION_UP,
3487 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003488 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003489 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003490 }
3491
Jeff Brownbe1aa822011-07-27 16:04:54 -07003492 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3493 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3494 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3495 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3496 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3497 // Pointer is still within the space of the virtual key.
3498 return true;
3499 }
3500 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003501
Jeff Brownbe1aa822011-07-27 16:04:54 -07003502 // Pointer left virtual key area or another pointer also went down.
3503 // Send key cancellation but do not consume the touch yet.
3504 // This is useful when the user swipes through from the virtual key area
3505 // into the main display surface.
3506 mCurrentVirtualKey.down = false;
3507 if (!mCurrentVirtualKey.ignored) {
3508#if DEBUG_VIRTUAL_KEYS
3509 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3510 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3511#endif
3512 dispatchVirtualKey(when, policyFlags,
3513 AKEY_EVENT_ACTION_UP,
3514 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3515 | AKEY_EVENT_FLAG_CANCELED);
3516 }
3517 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003518
Jeff Brownbe1aa822011-07-27 16:04:54 -07003519 if (mLastRawPointerData.touchingIdBits.isEmpty()
3520 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3521 // Pointer just went down. Check for virtual key press or off-screen touches.
3522 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3523 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3524 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3525 // If exactly one pointer went down, check for virtual key hit.
3526 // Otherwise we will drop the entire stroke.
3527 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3528 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3529 if (virtualKey) {
3530 mCurrentVirtualKey.down = true;
3531 mCurrentVirtualKey.downTime = when;
3532 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3533 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3534 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3535 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3536
3537 if (!mCurrentVirtualKey.ignored) {
3538#if DEBUG_VIRTUAL_KEYS
3539 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3540 mCurrentVirtualKey.keyCode,
3541 mCurrentVirtualKey.scanCode);
3542#endif
3543 dispatchVirtualKey(when, policyFlags,
3544 AKEY_EVENT_ACTION_DOWN,
3545 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3546 }
3547 }
3548 }
3549 return true;
3550 }
3551 }
3552
Jeff Brownfe508922011-01-18 15:10:10 -08003553 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003554 // most recent touch within the screen area. The idea is to filter out stray
3555 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003556 //
3557 // Problems we're trying to solve:
3558 //
3559 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3560 // virtual key area that is implemented by a separate touch panel and accidentally
3561 // triggers a virtual key.
3562 //
3563 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3564 // area and accidentally triggers a virtual key. This often happens when virtual keys
3565 // are layed out below the screen near to where the on screen keyboard's space bar
3566 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003567 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003568 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003569 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003570 return false;
3571}
3572
3573void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3574 int32_t keyEventAction, int32_t keyEventFlags) {
3575 int32_t keyCode = mCurrentVirtualKey.keyCode;
3576 int32_t scanCode = mCurrentVirtualKey.scanCode;
3577 nsecs_t downTime = mCurrentVirtualKey.downTime;
3578 int32_t metaState = mContext->getGlobalMetaState();
3579 policyFlags |= POLICY_FLAG_VIRTUAL;
3580
3581 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3582 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3583 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003584}
3585
Jeff Brown6d0fec22010-07-23 21:28:06 -07003586void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003587 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3588 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003589 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003590 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003591
3592 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003593 if (!currentIdBits.isEmpty()) {
3594 // No pointer id changes so this is a move event.
3595 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003596 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003597 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3598 AMOTION_EVENT_EDGE_FLAG_NONE,
3599 mCurrentCookedPointerData.pointerProperties,
3600 mCurrentCookedPointerData.pointerCoords,
3601 mCurrentCookedPointerData.idToIndex,
3602 currentIdBits, -1,
3603 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3604 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003605 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003606 // There may be pointers going up and pointers going down and pointers moving
3607 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003608 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3609 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003610 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003611 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003612
Jeff Brownace13b12011-03-09 17:39:48 -08003613 // Update last coordinates of pointers that have moved so that we observe the new
3614 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003615 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003616 mCurrentCookedPointerData.pointerProperties,
3617 mCurrentCookedPointerData.pointerCoords,
3618 mCurrentCookedPointerData.idToIndex,
3619 mLastCookedPointerData.pointerProperties,
3620 mLastCookedPointerData.pointerCoords,
3621 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003622 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003623 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003624 moveNeeded = true;
3625 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003626
Jeff Brownace13b12011-03-09 17:39:48 -08003627 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003628 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003629 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003630
Jeff Brown65fd2512011-08-18 11:20:58 -07003631 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003632 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003633 mLastCookedPointerData.pointerProperties,
3634 mLastCookedPointerData.pointerCoords,
3635 mLastCookedPointerData.idToIndex,
3636 dispatchedIdBits, upId,
3637 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003638 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003639 }
3640
Jeff Brownc3db8582010-10-20 15:33:38 -07003641 // Dispatch move events if any of the remaining pointers moved from their old locations.
3642 // Although applications receive new locations as part of individual pointer up
3643 // events, they do not generally handle them except when presented in a move event.
3644 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003645 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003646 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003647 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003648 mCurrentCookedPointerData.pointerProperties,
3649 mCurrentCookedPointerData.pointerCoords,
3650 mCurrentCookedPointerData.idToIndex,
3651 dispatchedIdBits, -1,
3652 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003653 }
3654
3655 // Dispatch pointer down events using the new pointer locations.
3656 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003657 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003658 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003659
Jeff Brownace13b12011-03-09 17:39:48 -08003660 if (dispatchedIdBits.count() == 1) {
3661 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003662 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003663 }
3664
Jeff Brown65fd2512011-08-18 11:20:58 -07003665 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003666 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003667 mCurrentCookedPointerData.pointerProperties,
3668 mCurrentCookedPointerData.pointerCoords,
3669 mCurrentCookedPointerData.idToIndex,
3670 dispatchedIdBits, downId,
3671 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003672 }
3673 }
Jeff Brownace13b12011-03-09 17:39:48 -08003674}
3675
Jeff Brownbe1aa822011-07-27 16:04:54 -07003676void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3677 if (mSentHoverEnter &&
3678 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3679 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3680 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003681 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003682 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3683 mLastCookedPointerData.pointerProperties,
3684 mLastCookedPointerData.pointerCoords,
3685 mLastCookedPointerData.idToIndex,
3686 mLastCookedPointerData.hoveringIdBits, -1,
3687 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3688 mSentHoverEnter = false;
3689 }
3690}
Jeff Brownace13b12011-03-09 17:39:48 -08003691
Jeff Brownbe1aa822011-07-27 16:04:54 -07003692void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3693 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3694 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3695 int32_t metaState = getContext()->getGlobalMetaState();
3696 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003697 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003698 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3699 mCurrentCookedPointerData.pointerProperties,
3700 mCurrentCookedPointerData.pointerCoords,
3701 mCurrentCookedPointerData.idToIndex,
3702 mCurrentCookedPointerData.hoveringIdBits, -1,
3703 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3704 mSentHoverEnter = true;
3705 }
Jeff Brownace13b12011-03-09 17:39:48 -08003706
Jeff Brown65fd2512011-08-18 11:20:58 -07003707 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003708 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3709 mCurrentCookedPointerData.pointerProperties,
3710 mCurrentCookedPointerData.pointerCoords,
3711 mCurrentCookedPointerData.idToIndex,
3712 mCurrentCookedPointerData.hoveringIdBits, -1,
3713 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3714 }
3715}
3716
3717void TouchInputMapper::cookPointerData() {
3718 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3719
3720 mCurrentCookedPointerData.clear();
3721 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3722 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3723 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3724
3725 // Walk through the the active pointers and map device coordinates onto
3726 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003727 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003728 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003729
Jeff Browna1f89ce2011-08-11 00:05:01 -07003730 // Size
3731 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3732 switch (mCalibration.sizeCalibration) {
3733 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3734 case Calibration::SIZE_CALIBRATION_DIAMETER:
3735 case Calibration::SIZE_CALIBRATION_AREA:
3736 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3737 touchMajor = in.touchMajor;
3738 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3739 toolMajor = in.toolMajor;
3740 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3741 size = mRawPointerAxes.touchMinor.valid
3742 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3743 } else if (mRawPointerAxes.touchMajor.valid) {
3744 toolMajor = touchMajor = in.touchMajor;
3745 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3746 ? in.touchMinor : in.touchMajor;
3747 size = mRawPointerAxes.touchMinor.valid
3748 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3749 } else if (mRawPointerAxes.toolMajor.valid) {
3750 touchMajor = toolMajor = in.toolMajor;
3751 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3752 ? in.toolMinor : in.toolMajor;
3753 size = mRawPointerAxes.toolMinor.valid
3754 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003755 } else {
Jeff Browna1f89ce2011-08-11 00:05:01 -07003756 LOG_ASSERT(false, "No touch or tool axes. "
3757 "Size calibration should have been resolved to NONE.");
3758 touchMajor = 0;
3759 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003760 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003761 toolMinor = 0;
3762 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003763 }
Jeff Brownace13b12011-03-09 17:39:48 -08003764
Jeff Browna1f89ce2011-08-11 00:05:01 -07003765 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3766 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3767 if (touchingCount > 1) {
3768 touchMajor /= touchingCount;
3769 touchMinor /= touchingCount;
3770 toolMajor /= touchingCount;
3771 toolMinor /= touchingCount;
3772 size /= touchingCount;
3773 }
3774 }
Jeff Brownace13b12011-03-09 17:39:48 -08003775
Jeff Browna1f89ce2011-08-11 00:05:01 -07003776 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3777 touchMajor *= mGeometricScale;
3778 touchMinor *= mGeometricScale;
3779 toolMajor *= mGeometricScale;
3780 toolMinor *= mGeometricScale;
3781 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3782 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003783 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003784 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3785 toolMinor = toolMajor;
3786 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3787 touchMinor = touchMajor;
3788 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003789 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003790
3791 mCalibration.applySizeScaleAndBias(&touchMajor);
3792 mCalibration.applySizeScaleAndBias(&touchMinor);
3793 mCalibration.applySizeScaleAndBias(&toolMajor);
3794 mCalibration.applySizeScaleAndBias(&toolMinor);
3795 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003796 break;
3797 default:
3798 touchMajor = 0;
3799 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003800 toolMajor = 0;
3801 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003802 size = 0;
3803 break;
3804 }
3805
Jeff Browna1f89ce2011-08-11 00:05:01 -07003806 // Pressure
3807 float pressure;
3808 switch (mCalibration.pressureCalibration) {
3809 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3810 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3811 pressure = in.pressure * mPressureScale;
3812 break;
3813 default:
3814 pressure = in.isHovering ? 0 : 1;
3815 break;
3816 }
3817
Jeff Brown65fd2512011-08-18 11:20:58 -07003818 // Tilt and Orientation
3819 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003820 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003821 if (mHaveTilt) {
3822 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3823 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3824 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3825 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3826 } else {
3827 tilt = 0;
3828
3829 switch (mCalibration.orientationCalibration) {
3830 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3831 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3832 break;
3833 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3834 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3835 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3836 if (c1 != 0 || c2 != 0) {
3837 orientation = atan2f(c1, c2) * 0.5f;
3838 float confidence = hypotf(c1, c2);
3839 float scale = 1.0f + confidence / 16.0f;
3840 touchMajor *= scale;
3841 touchMinor /= scale;
3842 toolMajor *= scale;
3843 toolMinor /= scale;
3844 } else {
3845 orientation = 0;
3846 }
3847 break;
3848 }
3849 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003850 orientation = 0;
3851 }
Jeff Brownace13b12011-03-09 17:39:48 -08003852 }
3853
Jeff Brown80fd47c2011-05-24 01:07:44 -07003854 // Distance
3855 float distance;
3856 switch (mCalibration.distanceCalibration) {
3857 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003858 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003859 break;
3860 default:
3861 distance = 0;
3862 }
3863
Jeff Brownace13b12011-03-09 17:39:48 -08003864 // X and Y
3865 // Adjust coords for surface orientation.
3866 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003867 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003868 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003869 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3870 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003871 orientation -= M_PI_2;
3872 if (orientation < - M_PI_2) {
3873 orientation += M_PI;
3874 }
3875 break;
3876 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003877 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3878 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003879 break;
3880 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003881 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3882 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003883 orientation += M_PI_2;
3884 if (orientation > M_PI_2) {
3885 orientation -= M_PI;
3886 }
3887 break;
3888 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003889 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3890 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003891 break;
3892 }
3893
3894 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003895 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003896 out.clear();
3897 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3898 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3899 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3900 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3901 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3902 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3903 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3904 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3905 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003906 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003907 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003908
3909 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003910 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3911 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003912 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003913 properties.id = id;
3914 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003915
Jeff Brownbe1aa822011-07-27 16:04:54 -07003916 // Write id index.
3917 mCurrentCookedPointerData.idToIndex[id] = i;
3918 }
Jeff Brownace13b12011-03-09 17:39:48 -08003919}
3920
Jeff Brown65fd2512011-08-18 11:20:58 -07003921void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3922 PointerUsage pointerUsage) {
3923 if (pointerUsage != mPointerUsage) {
3924 abortPointerUsage(when, policyFlags);
3925 mPointerUsage = pointerUsage;
3926 }
3927
3928 switch (mPointerUsage) {
3929 case POINTER_USAGE_GESTURES:
3930 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3931 break;
3932 case POINTER_USAGE_STYLUS:
3933 dispatchPointerStylus(when, policyFlags);
3934 break;
3935 case POINTER_USAGE_MOUSE:
3936 dispatchPointerMouse(when, policyFlags);
3937 break;
3938 default:
3939 break;
3940 }
3941}
3942
3943void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3944 switch (mPointerUsage) {
3945 case POINTER_USAGE_GESTURES:
3946 abortPointerGestures(when, policyFlags);
3947 break;
3948 case POINTER_USAGE_STYLUS:
3949 abortPointerStylus(when, policyFlags);
3950 break;
3951 case POINTER_USAGE_MOUSE:
3952 abortPointerMouse(when, policyFlags);
3953 break;
3954 default:
3955 break;
3956 }
3957
3958 mPointerUsage = POINTER_USAGE_NONE;
3959}
3960
Jeff Brown79ac9692011-04-19 21:20:10 -07003961void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3962 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003963 // Update current gesture coordinates.
3964 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003965 bool sendEvents = preparePointerGestures(when,
3966 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3967 if (!sendEvents) {
3968 return;
3969 }
Jeff Brown19c97d42011-06-01 12:33:19 -07003970 if (finishPreviousGesture) {
3971 cancelPreviousGesture = false;
3972 }
Jeff Brownace13b12011-03-09 17:39:48 -08003973
Jeff Brownbb3fcba2011-06-06 19:23:05 -07003974 // Update the pointer presentation and spots.
3975 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3976 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3977 if (finishPreviousGesture || cancelPreviousGesture) {
3978 mPointerController->clearSpots();
3979 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003980 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3981 mPointerGesture.currentGestureIdToIndex,
3982 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07003983 } else {
3984 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3985 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003986
Jeff Brown538881e2011-05-25 18:23:38 -07003987 // Show or hide the pointer if needed.
3988 switch (mPointerGesture.currentGestureMode) {
3989 case PointerGesture::NEUTRAL:
3990 case PointerGesture::QUIET:
3991 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3992 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3993 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3994 // Remind the user of where the pointer is after finishing a gesture with spots.
3995 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3996 }
3997 break;
3998 case PointerGesture::TAP:
3999 case PointerGesture::TAP_DRAG:
4000 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4001 case PointerGesture::HOVER:
4002 case PointerGesture::PRESS:
4003 // Unfade the pointer when the current gesture manipulates the
4004 // area directly under the pointer.
4005 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4006 break;
4007 case PointerGesture::SWIPE:
4008 case PointerGesture::FREEFORM:
4009 // Fade the pointer when the current gesture manipulates a different
4010 // area and there are spots to guide the user experience.
4011 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4012 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4013 } else {
4014 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4015 }
4016 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004017 }
4018
Jeff Brownace13b12011-03-09 17:39:48 -08004019 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004020 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004021 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004022
4023 // Update last coordinates of pointers that have moved so that we observe the new
4024 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004025 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4026 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4027 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004028 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004029 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4030 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4031 bool moveNeeded = false;
4032 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004033 && !mPointerGesture.lastGestureIdBits.isEmpty()
4034 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004035 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4036 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004037 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004038 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004039 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004040 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4041 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004042 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004043 moveNeeded = true;
4044 }
Jeff Brownace13b12011-03-09 17:39:48 -08004045 }
4046
4047 // Send motion events for all pointers that went up or were canceled.
4048 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4049 if (!dispatchedGestureIdBits.isEmpty()) {
4050 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004051 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004052 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4053 AMOTION_EVENT_EDGE_FLAG_NONE,
4054 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004055 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4056 dispatchedGestureIdBits, -1,
4057 0, 0, mPointerGesture.downTime);
4058
4059 dispatchedGestureIdBits.clear();
4060 } else {
4061 BitSet32 upGestureIdBits;
4062 if (finishPreviousGesture) {
4063 upGestureIdBits = dispatchedGestureIdBits;
4064 } else {
4065 upGestureIdBits.value = dispatchedGestureIdBits.value
4066 & ~mPointerGesture.currentGestureIdBits.value;
4067 }
4068 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004069 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004070
Jeff Brown65fd2512011-08-18 11:20:58 -07004071 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004072 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004073 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4074 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004075 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4076 dispatchedGestureIdBits, id,
4077 0, 0, mPointerGesture.downTime);
4078
4079 dispatchedGestureIdBits.clearBit(id);
4080 }
4081 }
4082 }
4083
4084 // Send motion events for all pointers that moved.
4085 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004086 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004087 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4088 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004089 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4090 dispatchedGestureIdBits, -1,
4091 0, 0, mPointerGesture.downTime);
4092 }
4093
4094 // Send motion events for all pointers that went down.
4095 if (down) {
4096 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4097 & ~dispatchedGestureIdBits.value);
4098 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004099 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004100 dispatchedGestureIdBits.markBit(id);
4101
Jeff Brownace13b12011-03-09 17:39:48 -08004102 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004103 mPointerGesture.downTime = when;
4104 }
4105
Jeff Brown65fd2512011-08-18 11:20:58 -07004106 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004107 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004108 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004109 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4110 dispatchedGestureIdBits, id,
4111 0, 0, mPointerGesture.downTime);
4112 }
4113 }
4114
Jeff Brownace13b12011-03-09 17:39:48 -08004115 // Send motion events for hover.
4116 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004117 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004118 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4119 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4120 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004121 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4122 mPointerGesture.currentGestureIdBits, -1,
4123 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004124 } else if (dispatchedGestureIdBits.isEmpty()
4125 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4126 // Synthesize a hover move event after all pointers go up to indicate that
4127 // the pointer is hovering again even if the user is not currently touching
4128 // the touch pad. This ensures that a view will receive a fresh hover enter
4129 // event after a tap.
4130 float x, y;
4131 mPointerController->getPosition(&x, &y);
4132
4133 PointerProperties pointerProperties;
4134 pointerProperties.clear();
4135 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004136 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004137
4138 PointerCoords pointerCoords;
4139 pointerCoords.clear();
4140 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4141 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4142
Jeff Brown65fd2512011-08-18 11:20:58 -07004143 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004144 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4145 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4146 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004147 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004148 }
4149
4150 // Update state.
4151 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4152 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004153 mPointerGesture.lastGestureIdBits.clear();
4154 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004155 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4156 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004157 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004158 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004159 mPointerGesture.lastGestureProperties[index].copyFrom(
4160 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004161 mPointerGesture.lastGestureCoords[index].copyFrom(
4162 mPointerGesture.currentGestureCoords[index]);
4163 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004164 }
4165 }
4166}
4167
Jeff Brown65fd2512011-08-18 11:20:58 -07004168void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4169 // Cancel previously dispatches pointers.
4170 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4171 int32_t metaState = getContext()->getGlobalMetaState();
4172 int32_t buttonState = mCurrentButtonState;
4173 dispatchMotion(when, policyFlags, mSource,
4174 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4175 AMOTION_EVENT_EDGE_FLAG_NONE,
4176 mPointerGesture.lastGestureProperties,
4177 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4178 mPointerGesture.lastGestureIdBits, -1,
4179 0, 0, mPointerGesture.downTime);
4180 }
4181
4182 // Reset the current pointer gesture.
4183 mPointerGesture.reset();
4184 mPointerVelocityControl.reset();
4185
4186 // Remove any current spots.
4187 if (mPointerController != NULL) {
4188 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4189 mPointerController->clearSpots();
4190 }
4191}
4192
Jeff Brown79ac9692011-04-19 21:20:10 -07004193bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4194 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004195 *outCancelPreviousGesture = false;
4196 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004197
Jeff Brown79ac9692011-04-19 21:20:10 -07004198 // Handle TAP timeout.
4199 if (isTimeout) {
4200#if DEBUG_GESTURES
4201 LOGD("Gestures: Processing timeout");
4202#endif
4203
4204 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004205 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004206 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004207 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004208 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004209 } else {
4210 // The tap is finished.
4211#if DEBUG_GESTURES
4212 LOGD("Gestures: TAP finished");
4213#endif
4214 *outFinishPreviousGesture = true;
4215
4216 mPointerGesture.activeGestureId = -1;
4217 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4218 mPointerGesture.currentGestureIdBits.clear();
4219
Jeff Brown65fd2512011-08-18 11:20:58 -07004220 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004221 return true;
4222 }
4223 }
4224
4225 // We did not handle this timeout.
4226 return false;
4227 }
4228
Jeff Brown65fd2512011-08-18 11:20:58 -07004229 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4230 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4231
Jeff Brownace13b12011-03-09 17:39:48 -08004232 // Update the velocity tracker.
4233 {
4234 VelocityTracker::Position positions[MAX_POINTERS];
4235 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004236 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004237 uint32_t id = idBits.clearFirstMarkedBit();
4238 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004239 positions[count].x = pointer.x * mPointerXMovementScale;
4240 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004241 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004242 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004243 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004244 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004245
Jeff Brownace13b12011-03-09 17:39:48 -08004246 // Pick a new active touch id if needed.
4247 // Choose an arbitrary pointer that just went down, if there is one.
4248 // Otherwise choose an arbitrary remaining pointer.
4249 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004250 // We keep the same active touch id for as long as possible.
4251 bool activeTouchChanged = false;
4252 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4253 int32_t activeTouchId = lastActiveTouchId;
4254 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004255 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004256 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004257 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004258 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004259 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004260 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004261 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004262 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004263 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004264 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004265 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004266 } else {
4267 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004268 }
4269 }
4270
4271 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004272 bool isQuietTime = false;
4273 if (activeTouchId < 0) {
4274 mPointerGesture.resetQuietTime();
4275 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004276 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004277 if (!isQuietTime) {
4278 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4279 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4280 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004281 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004282 // Enter quiet time when exiting swipe or freeform state.
4283 // This is to prevent accidentally entering the hover state and flinging the
4284 // pointer when finishing a swipe and there is still one pointer left onscreen.
4285 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004286 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004287 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004288 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004289 // Enter quiet time when releasing the button and there are still two or more
4290 // fingers down. This may indicate that one finger was used to press the button
4291 // but it has not gone up yet.
4292 isQuietTime = true;
4293 }
4294 if (isQuietTime) {
4295 mPointerGesture.quietTime = when;
4296 }
Jeff Brownace13b12011-03-09 17:39:48 -08004297 }
4298 }
4299
4300 // Switch states based on button and pointer state.
4301 if (isQuietTime) {
4302 // Case 1: Quiet time. (QUIET)
4303#if DEBUG_GESTURES
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004304 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004305 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004306#endif
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004307 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4308 *outFinishPreviousGesture = true;
4309 }
Jeff Brownace13b12011-03-09 17:39:48 -08004310
4311 mPointerGesture.activeGestureId = -1;
4312 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004313 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004314
Jeff Brown65fd2512011-08-18 11:20:58 -07004315 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004316 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004317 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004318 // The pointer follows the active touch point.
4319 // Emit DOWN, MOVE, UP events at the pointer location.
4320 //
4321 // Only the active touch matters; other fingers are ignored. This policy helps
4322 // to handle the case where the user places a second finger on the touch pad
4323 // to apply the necessary force to depress an integrated button below the surface.
4324 // We don't want the second finger to be delivered to applications.
4325 //
4326 // For this to work well, we need to make sure to track the pointer that is really
4327 // active. If the user first puts one finger down to click then adds another
4328 // finger to drag then the active pointer should switch to the finger that is
4329 // being dragged.
4330#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004331 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004332 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004333#endif
4334 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004335 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004336 *outFinishPreviousGesture = true;
4337 mPointerGesture.activeGestureId = 0;
4338 }
4339
4340 // Switch pointers if needed.
4341 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004342 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d42011-06-01 12:33:19 -07004343 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004344 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004345 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004346 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d42011-06-01 12:33:19 -07004347 float vx, vy;
4348 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4349 float speed = hypotf(vx, vy);
4350 if (speed > bestSpeed) {
4351 bestId = id;
4352 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004353 }
Jeff Brown8d608662010-08-30 03:02:23 -07004354 }
Jeff Brown19c97d42011-06-01 12:33:19 -07004355 }
4356 if (bestId >= 0 && bestId != activeTouchId) {
4357 mPointerGesture.activeTouchId = activeTouchId = bestId;
4358 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004359#if DEBUG_GESTURES
Jeff Brown19c97d42011-06-01 12:33:19 -07004360 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4361 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004362#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004363 }
Jeff Brown19c97d42011-06-01 12:33:19 -07004364 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004365
Jeff Brown65fd2512011-08-18 11:20:58 -07004366 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004367 const RawPointerData::Pointer& currentPointer =
4368 mCurrentRawPointerData.pointerForId(activeTouchId);
4369 const RawPointerData::Pointer& lastPointer =
4370 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004371 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4372 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004373
Jeff Brownbe1aa822011-07-27 16:04:54 -07004374 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004375 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d42011-06-01 12:33:19 -07004376
4377 // Move the pointer using a relative motion.
4378 // When using spots, the click will occur at the position of the anchor
4379 // spot and all other spots will move there.
4380 mPointerController->move(deltaX, deltaY);
4381 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004382 mPointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004383 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004384
Jeff Brownace13b12011-03-09 17:39:48 -08004385 float x, y;
4386 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004387
Jeff Brown79ac9692011-04-19 21:20:10 -07004388 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004389 mPointerGesture.currentGestureIdBits.clear();
4390 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4391 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004392 mPointerGesture.currentGestureProperties[0].clear();
4393 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004394 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004395 mPointerGesture.currentGestureCoords[0].clear();
4396 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4397 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4398 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004399 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004400 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004401 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4402 *outFinishPreviousGesture = true;
4403 }
Jeff Brownace13b12011-03-09 17:39:48 -08004404
Jeff Brown79ac9692011-04-19 21:20:10 -07004405 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004406 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004407 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004408 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4409 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004410 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004411 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004412 float x, y;
4413 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004414 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4415 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004416#if DEBUG_GESTURES
4417 LOGD("Gestures: TAP");
4418#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004419
4420 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004421 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004422 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004423
Jeff Brownace13b12011-03-09 17:39:48 -08004424 mPointerGesture.activeGestureId = 0;
4425 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004426 mPointerGesture.currentGestureIdBits.clear();
4427 mPointerGesture.currentGestureIdBits.markBit(
4428 mPointerGesture.activeGestureId);
4429 mPointerGesture.currentGestureIdToIndex[
4430 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004431 mPointerGesture.currentGestureProperties[0].clear();
4432 mPointerGesture.currentGestureProperties[0].id =
4433 mPointerGesture.activeGestureId;
4434 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004435 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004436 mPointerGesture.currentGestureCoords[0].clear();
4437 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004438 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004439 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004440 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004441 mPointerGesture.currentGestureCoords[0].setAxisValue(
4442 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004443
Jeff Brownace13b12011-03-09 17:39:48 -08004444 tapped = true;
4445 } else {
4446#if DEBUG_GESTURES
4447 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004448 x - mPointerGesture.tapX,
4449 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004450#endif
4451 }
4452 } else {
4453#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004454 LOGD("Gestures: Not a TAP, %0.3fms since down",
4455 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004456#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004457 }
Jeff Brownace13b12011-03-09 17:39:48 -08004458 }
Jeff Brown2352b972011-04-12 22:39:53 -07004459
Jeff Brown65fd2512011-08-18 11:20:58 -07004460 mPointerVelocityControl.reset();
Jeff Brown19c97d42011-06-01 12:33:19 -07004461
Jeff Brownace13b12011-03-09 17:39:48 -08004462 if (!tapped) {
4463#if DEBUG_GESTURES
4464 LOGD("Gestures: NEUTRAL");
4465#endif
4466 mPointerGesture.activeGestureId = -1;
4467 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004468 mPointerGesture.currentGestureIdBits.clear();
4469 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004470 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004471 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004472 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004473 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4474 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004475 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004476
Jeff Brown79ac9692011-04-19 21:20:10 -07004477 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4478 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004479 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004480 float x, y;
4481 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004482 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4483 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004484 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4485 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004486#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004487 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4488 x - mPointerGesture.tapX,
4489 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004490#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004491 }
4492 } else {
4493#if DEBUG_GESTURES
4494 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4495 (when - mPointerGesture.tapUpTime) * 0.000001f);
4496#endif
4497 }
4498 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4499 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4500 }
Jeff Brownace13b12011-03-09 17:39:48 -08004501
Jeff Brown65fd2512011-08-18 11:20:58 -07004502 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004503 const RawPointerData::Pointer& currentPointer =
4504 mCurrentRawPointerData.pointerForId(activeTouchId);
4505 const RawPointerData::Pointer& lastPointer =
4506 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004507 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004508 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004509 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004510 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004511
Jeff Brownbe1aa822011-07-27 16:04:54 -07004512 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004513 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d42011-06-01 12:33:19 -07004514
Jeff Brown2352b972011-04-12 22:39:53 -07004515 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004516 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004517 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d42011-06-01 12:33:19 -07004518 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004519 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004520 }
4521
Jeff Brown79ac9692011-04-19 21:20:10 -07004522 bool down;
4523 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4524#if DEBUG_GESTURES
4525 LOGD("Gestures: TAP_DRAG");
4526#endif
4527 down = true;
4528 } else {
4529#if DEBUG_GESTURES
4530 LOGD("Gestures: HOVER");
4531#endif
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004532 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4533 *outFinishPreviousGesture = true;
4534 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004535 mPointerGesture.activeGestureId = 0;
4536 down = false;
4537 }
Jeff Brownace13b12011-03-09 17:39:48 -08004538
4539 float x, y;
4540 mPointerController->getPosition(&x, &y);
4541
Jeff Brownace13b12011-03-09 17:39:48 -08004542 mPointerGesture.currentGestureIdBits.clear();
4543 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4544 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004545 mPointerGesture.currentGestureProperties[0].clear();
4546 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4547 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004548 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004549 mPointerGesture.currentGestureCoords[0].clear();
4550 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4551 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004552 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4553 down ? 1.0f : 0.0f);
4554
Jeff Brown65fd2512011-08-18 11:20:58 -07004555 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004556 mPointerGesture.resetTap();
4557 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004558 mPointerGesture.tapX = x;
4559 mPointerGesture.tapY = y;
4560 }
Jeff Brownace13b12011-03-09 17:39:48 -08004561 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004562 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4563 // We need to provide feedback for each finger that goes down so we cannot wait
4564 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004565 //
Jeff Brown2352b972011-04-12 22:39:53 -07004566 // The ambiguous case is deciding what to do when there are two fingers down but they
4567 // have not moved enough to determine whether they are part of a drag or part of a
4568 // freeform gesture, or just a press or long-press at the pointer location.
4569 //
4570 // When there are two fingers we start with the PRESS hypothesis and we generate a
4571 // down at the pointer location.
4572 //
4573 // When the two fingers move enough or when additional fingers are added, we make
4574 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004575 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004576
Jeff Brown214eaf42011-05-26 19:17:02 -07004577 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004578 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004579 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004580 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4581 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004582 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004583 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d42011-06-01 12:33:19 -07004584 // Additional pointers have gone down but not yet settled.
4585 // Reset the gesture.
4586#if DEBUG_GESTURES
4587 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004588 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004589 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d42011-06-01 12:33:19 -07004590 * 0.000001f);
4591#endif
4592 *outCancelPreviousGesture = true;
4593 } else {
4594 // Continue previous gesture.
4595 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4596 }
4597
4598 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004599 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4600 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004601 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004602 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004603
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004604 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004605#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004606 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4607 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004608 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004609 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004610#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004611 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4612 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004613 &mPointerGesture.referenceTouchY);
4614 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4615 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004616 }
Jeff Brownace13b12011-03-09 17:39:48 -08004617
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004618 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004619 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004620 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4621 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004622 mPointerGesture.referenceDeltas[id].dx = 0;
4623 mPointerGesture.referenceDeltas[id].dy = 0;
4624 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004625 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004626
4627 // Add delta for all fingers and calculate a common movement delta.
4628 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004629 BitSet32 commonIdBits(mLastFingerIdBits.value
4630 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004631 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4632 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004633 uint32_t id = idBits.clearFirstMarkedBit();
4634 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4635 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004636 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4637 delta.dx += cpd.x - lpd.x;
4638 delta.dy += cpd.y - lpd.y;
4639
4640 if (first) {
4641 commonDeltaX = delta.dx;
4642 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004643 } else {
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004644 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4645 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4646 }
4647 }
Jeff Brownace13b12011-03-09 17:39:48 -08004648
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004649 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4650 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4651 float dist[MAX_POINTER_ID + 1];
4652 int32_t distOverThreshold = 0;
4653 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004654 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004655 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004656 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4657 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004658 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004659 distOverThreshold += 1;
4660 }
4661 }
4662
4663 // Only transition when at least two pointers have moved further than
4664 // the minimum distance threshold.
4665 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004666 if (currentFingerCount > 2) {
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004667 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004668#if DEBUG_GESTURES
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004669 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004670 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004671#endif
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004672 *outCancelPreviousGesture = true;
4673 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4674 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004675 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004676 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004677 uint32_t id1 = idBits.clearFirstMarkedBit();
4678 uint32_t id2 = idBits.firstMarkedBit();
4679 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4680 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4681 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4682 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4683 // There are two pointers but they are too far apart for a SWIPE,
4684 // switch to FREEFORM.
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004685#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004686 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4687 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004688#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004689 *outCancelPreviousGesture = true;
4690 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4691 } else {
4692 // There are two pointers. Wait for both pointers to start moving
4693 // before deciding whether this is a SWIPE or FREEFORM gesture.
4694 float dist1 = dist[id1];
4695 float dist2 = dist[id2];
4696 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4697 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4698 // Calculate the dot product of the displacement vectors.
4699 // When the vectors are oriented in approximately the same direction,
4700 // the angle betweeen them is near zero and the cosine of the angle
4701 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4702 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4703 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004704 float dx1 = delta1.dx * mPointerXZoomScale;
4705 float dy1 = delta1.dy * mPointerYZoomScale;
4706 float dx2 = delta2.dx * mPointerXZoomScale;
4707 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004708 float dot = dx1 * dx2 + dy1 * dy2;
4709 float cosine = dot / (dist1 * dist2); // denominator always > 0
4710 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4711 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004712#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004713 LOGD("Gestures: PRESS transitioned to SWIPE, "
4714 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4715 "cosine %0.3f >= %0.3f",
4716 dist1, mConfig.pointerGestureMultitouchMinDistance,
4717 dist2, mConfig.pointerGestureMultitouchMinDistance,
4718 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004719#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004720 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4721 } else {
4722 // Pointers are moving in different directions. Switch to FREEFORM.
4723#if DEBUG_GESTURES
4724 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4725 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4726 "cosine %0.3f < %0.3f",
4727 dist1, mConfig.pointerGestureMultitouchMinDistance,
4728 dist2, mConfig.pointerGestureMultitouchMinDistance,
4729 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4730#endif
4731 *outCancelPreviousGesture = true;
4732 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4733 }
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004734 }
Jeff Brownace13b12011-03-09 17:39:48 -08004735 }
4736 }
Jeff Brownace13b12011-03-09 17:39:48 -08004737 }
4738 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004739 // Switch from SWIPE to FREEFORM if additional pointers go down.
4740 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004741 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004742#if DEBUG_GESTURES
4743 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004744 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004745#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004746 *outCancelPreviousGesture = true;
4747 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004748 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004749 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004750
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004751 // Move the reference points based on the overall group motion of the fingers
4752 // except in PRESS mode while waiting for a transition to occur.
4753 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4754 && (commonDeltaX || commonDeltaY)) {
4755 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004756 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004757 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004758 delta.dx = 0;
4759 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004760 }
4761
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004762 mPointerGesture.referenceTouchX += commonDeltaX;
4763 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004764
Jeff Brown65fd2512011-08-18 11:20:58 -07004765 commonDeltaX *= mPointerXMovementScale;
4766 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004767
Jeff Brownbe1aa822011-07-27 16:04:54 -07004768 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004769 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004770
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004771 mPointerGesture.referenceGestureX += commonDeltaX;
4772 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004773 }
4774
4775 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004776 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4777 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4778 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004779#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004780 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004781 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004782 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004783#endif
4784 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4785
4786 mPointerGesture.currentGestureIdBits.clear();
4787 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4788 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004789 mPointerGesture.currentGestureProperties[0].clear();
4790 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4791 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004792 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004793 mPointerGesture.currentGestureCoords[0].clear();
4794 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4795 mPointerGesture.referenceGestureX);
4796 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4797 mPointerGesture.referenceGestureY);
4798 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004799 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4800 // FREEFORM mode.
4801#if DEBUG_GESTURES
4802 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4803 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004804 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004805#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004806 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004807
Jeff Brownace13b12011-03-09 17:39:48 -08004808 mPointerGesture.currentGestureIdBits.clear();
4809
4810 BitSet32 mappedTouchIdBits;
4811 BitSet32 usedGestureIdBits;
4812 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4813 // Initially, assign the active gesture id to the active touch point
4814 // if there is one. No other touch id bits are mapped yet.
4815 if (!*outCancelPreviousGesture) {
4816 mappedTouchIdBits.markBit(activeTouchId);
4817 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4818 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4819 mPointerGesture.activeGestureId;
4820 } else {
4821 mPointerGesture.activeGestureId = -1;
4822 }
4823 } else {
4824 // Otherwise, assume we mapped all touches from the previous frame.
4825 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004826 mappedTouchIdBits.value = mLastFingerIdBits.value
4827 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004828 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4829
4830 // Check whether we need to choose a new active gesture id because the
4831 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004832 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4833 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004834 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004835 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004836 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4837 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4838 mPointerGesture.activeGestureId = -1;
4839 break;
4840 }
4841 }
4842 }
4843
4844#if DEBUG_GESTURES
4845 LOGD("Gestures: FREEFORM follow up "
4846 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4847 "activeGestureId=%d",
4848 mappedTouchIdBits.value, usedGestureIdBits.value,
4849 mPointerGesture.activeGestureId);
4850#endif
4851
Jeff Brown65fd2512011-08-18 11:20:58 -07004852 BitSet32 idBits(mCurrentFingerIdBits);
4853 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004854 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004855 uint32_t gestureId;
4856 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004857 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004858 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4859#if DEBUG_GESTURES
4860 LOGD("Gestures: FREEFORM "
4861 "new mapping for touch id %d -> gesture id %d",
4862 touchId, gestureId);
4863#endif
4864 } else {
4865 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4866#if DEBUG_GESTURES
4867 LOGD("Gestures: FREEFORM "
4868 "existing mapping for touch id %d -> gesture id %d",
4869 touchId, gestureId);
4870#endif
4871 }
4872 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4873 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4874
Jeff Brownbe1aa822011-07-27 16:04:54 -07004875 const RawPointerData::Pointer& pointer =
4876 mCurrentRawPointerData.pointerForId(touchId);
4877 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004878 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004879 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004880 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004881 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004882
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004883 mPointerGesture.currentGestureProperties[i].clear();
4884 mPointerGesture.currentGestureProperties[i].id = gestureId;
4885 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004886 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004887 mPointerGesture.currentGestureCoords[i].clear();
4888 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004889 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004890 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004891 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004892 mPointerGesture.currentGestureCoords[i].setAxisValue(
4893 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4894 }
4895
4896 if (mPointerGesture.activeGestureId < 0) {
4897 mPointerGesture.activeGestureId =
4898 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4899#if DEBUG_GESTURES
4900 LOGD("Gestures: FREEFORM new "
4901 "activeGestureId=%d", mPointerGesture.activeGestureId);
4902#endif
4903 }
Jeff Brown2352b972011-04-12 22:39:53 -07004904 }
Jeff Brownace13b12011-03-09 17:39:48 -08004905 }
4906
Jeff Brownbe1aa822011-07-27 16:04:54 -07004907 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004908
Jeff Brownace13b12011-03-09 17:39:48 -08004909#if DEBUG_GESTURES
4910 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004911 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4912 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004913 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004914 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4915 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004916 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004917 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004918 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004919 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004920 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004921 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4922 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4923 id, index, properties.toolType,
4924 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004925 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4926 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4927 }
4928 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004929 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004930 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004931 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004932 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004933 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4934 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4935 id, index, properties.toolType,
4936 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004937 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4938 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4939 }
4940#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004941 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004942}
4943
Jeff Brown65fd2512011-08-18 11:20:58 -07004944void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4945 mPointerSimple.currentCoords.clear();
4946 mPointerSimple.currentProperties.clear();
4947
4948 bool down, hovering;
4949 if (!mCurrentStylusIdBits.isEmpty()) {
4950 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4951 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4952 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4953 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4954 mPointerController->setPosition(x, y);
4955
4956 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4957 down = !hovering;
4958
4959 mPointerController->getPosition(&x, &y);
4960 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4961 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4962 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4963 mPointerSimple.currentProperties.id = 0;
4964 mPointerSimple.currentProperties.toolType =
4965 mCurrentCookedPointerData.pointerProperties[index].toolType;
4966 } else {
4967 down = false;
4968 hovering = false;
4969 }
4970
4971 dispatchPointerSimple(when, policyFlags, down, hovering);
4972}
4973
4974void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
4975 abortPointerSimple(when, policyFlags);
4976}
4977
4978void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
4979 mPointerSimple.currentCoords.clear();
4980 mPointerSimple.currentProperties.clear();
4981
4982 bool down, hovering;
4983 if (!mCurrentMouseIdBits.isEmpty()) {
4984 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
4985 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
4986 if (mLastMouseIdBits.hasBit(id)) {
4987 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
4988 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
4989 - mLastRawPointerData.pointers[lastIndex].x)
4990 * mPointerXMovementScale;
4991 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
4992 - mLastRawPointerData.pointers[lastIndex].y)
4993 * mPointerYMovementScale;
4994
4995 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
4996 mPointerVelocityControl.move(when, &deltaX, &deltaY);
4997
4998 mPointerController->move(deltaX, deltaY);
4999 } else {
5000 mPointerVelocityControl.reset();
5001 }
5002
5003 down = isPointerDown(mCurrentButtonState);
5004 hovering = !down;
5005
5006 float x, y;
5007 mPointerController->getPosition(&x, &y);
5008 mPointerSimple.currentCoords.copyFrom(
5009 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5010 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5011 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5012 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5013 hovering ? 0.0f : 1.0f);
5014 mPointerSimple.currentProperties.id = 0;
5015 mPointerSimple.currentProperties.toolType =
5016 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5017 } else {
5018 mPointerVelocityControl.reset();
5019
5020 down = false;
5021 hovering = false;
5022 }
5023
5024 dispatchPointerSimple(when, policyFlags, down, hovering);
5025}
5026
5027void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5028 abortPointerSimple(when, policyFlags);
5029
5030 mPointerVelocityControl.reset();
5031}
5032
5033void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5034 bool down, bool hovering) {
5035 int32_t metaState = getContext()->getGlobalMetaState();
5036
5037 if (mPointerController != NULL) {
5038 if (down || hovering) {
5039 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5040 mPointerController->clearSpots();
5041 mPointerController->setButtonState(mCurrentButtonState);
5042 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5043 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5044 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5045 }
5046 }
5047
5048 if (mPointerSimple.down && !down) {
5049 mPointerSimple.down = false;
5050
5051 // Send up.
5052 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5053 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5054 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5055 mOrientedXPrecision, mOrientedYPrecision,
5056 mPointerSimple.downTime);
5057 getListener()->notifyMotion(&args);
5058 }
5059
5060 if (mPointerSimple.hovering && !hovering) {
5061 mPointerSimple.hovering = false;
5062
5063 // Send hover exit.
5064 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5065 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5066 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5067 mOrientedXPrecision, mOrientedYPrecision,
5068 mPointerSimple.downTime);
5069 getListener()->notifyMotion(&args);
5070 }
5071
5072 if (down) {
5073 if (!mPointerSimple.down) {
5074 mPointerSimple.down = true;
5075 mPointerSimple.downTime = when;
5076
5077 // Send down.
5078 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5079 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5080 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5081 mOrientedXPrecision, mOrientedYPrecision,
5082 mPointerSimple.downTime);
5083 getListener()->notifyMotion(&args);
5084 }
5085
5086 // Send move.
5087 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5088 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5089 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5090 mOrientedXPrecision, mOrientedYPrecision,
5091 mPointerSimple.downTime);
5092 getListener()->notifyMotion(&args);
5093 }
5094
5095 if (hovering) {
5096 if (!mPointerSimple.hovering) {
5097 mPointerSimple.hovering = true;
5098
5099 // Send hover enter.
5100 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5101 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5102 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5103 mOrientedXPrecision, mOrientedYPrecision,
5104 mPointerSimple.downTime);
5105 getListener()->notifyMotion(&args);
5106 }
5107
5108 // Send hover move.
5109 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5110 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5111 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5112 mOrientedXPrecision, mOrientedYPrecision,
5113 mPointerSimple.downTime);
5114 getListener()->notifyMotion(&args);
5115 }
5116
5117 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5118 float vscroll = mCurrentRawVScroll;
5119 float hscroll = mCurrentRawHScroll;
5120 mWheelYVelocityControl.move(when, NULL, &vscroll);
5121 mWheelXVelocityControl.move(when, &hscroll, NULL);
5122
5123 // Send scroll.
5124 PointerCoords pointerCoords;
5125 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5126 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5127 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5128
5129 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5130 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5131 1, &mPointerSimple.currentProperties, &pointerCoords,
5132 mOrientedXPrecision, mOrientedYPrecision,
5133 mPointerSimple.downTime);
5134 getListener()->notifyMotion(&args);
5135 }
5136
5137 // Save state.
5138 if (down || hovering) {
5139 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5140 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5141 } else {
5142 mPointerSimple.reset();
5143 }
5144}
5145
5146void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5147 mPointerSimple.currentCoords.clear();
5148 mPointerSimple.currentProperties.clear();
5149
5150 dispatchPointerSimple(when, policyFlags, false, false);
5151}
5152
Jeff Brownace13b12011-03-09 17:39:48 -08005153void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005154 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5155 const PointerProperties* properties, const PointerCoords* coords,
5156 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005157 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5158 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005159 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005160 uint32_t pointerCount = 0;
5161 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005162 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005163 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005164 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005165 pointerCoords[pointerCount].copyFrom(coords[index]);
5166
5167 if (changedId >= 0 && id == uint32_t(changedId)) {
5168 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5169 }
5170
5171 pointerCount += 1;
5172 }
5173
Jeff Brownb6110c22011-04-01 16:15:13 -07005174 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005175
5176 if (changedId >= 0 && pointerCount == 1) {
5177 // Replace initial down and final up action.
5178 // We can compare the action without masking off the changed pointer index
5179 // because we know the index is 0.
5180 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5181 action = AMOTION_EVENT_ACTION_DOWN;
5182 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5183 action = AMOTION_EVENT_ACTION_UP;
5184 } else {
5185 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07005186 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005187 }
5188 }
5189
Jeff Brownbe1aa822011-07-27 16:04:54 -07005190 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005191 action, flags, metaState, buttonState, edgeFlags,
5192 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005193 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005194}
5195
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005196bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005197 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005198 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5199 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005200 bool changed = false;
5201 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005202 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005203 uint32_t inIndex = inIdToIndex[id];
5204 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005205
5206 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005207 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005208 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005209 PointerCoords& curOutCoords = outCoords[outIndex];
5210
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005211 if (curInProperties != curOutProperties) {
5212 curOutProperties.copyFrom(curInProperties);
5213 changed = true;
5214 }
5215
Jeff Brownace13b12011-03-09 17:39:48 -08005216 if (curInCoords != curOutCoords) {
5217 curOutCoords.copyFrom(curInCoords);
5218 changed = true;
5219 }
5220 }
5221 return changed;
5222}
5223
5224void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005225 if (mPointerController != NULL) {
5226 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5227 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005228}
5229
Jeff Brownbe1aa822011-07-27 16:04:54 -07005230bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5231 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5232 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005233}
5234
Jeff Brownbe1aa822011-07-27 16:04:54 -07005235const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005236 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005237 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005238 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005239 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005240
5241#if DEBUG_VIRTUAL_KEYS
5242 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
5243 "left=%d, top=%d, right=%d, bottom=%d",
5244 x, y,
5245 virtualKey.keyCode, virtualKey.scanCode,
5246 virtualKey.hitLeft, virtualKey.hitTop,
5247 virtualKey.hitRight, virtualKey.hitBottom);
5248#endif
5249
5250 if (virtualKey.isHit(x, y)) {
5251 return & virtualKey;
5252 }
5253 }
5254
5255 return NULL;
5256}
5257
Jeff Brownbe1aa822011-07-27 16:04:54 -07005258void TouchInputMapper::assignPointerIds() {
5259 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5260 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5261
5262 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005263
5264 if (currentPointerCount == 0) {
5265 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005266 return;
5267 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005268
Jeff Brownbe1aa822011-07-27 16:04:54 -07005269 if (lastPointerCount == 0) {
5270 // All pointers are new.
5271 for (uint32_t i = 0; i < currentPointerCount; i++) {
5272 uint32_t id = i;
5273 mCurrentRawPointerData.pointers[i].id = id;
5274 mCurrentRawPointerData.idToIndex[id] = i;
5275 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5276 }
5277 return;
5278 }
5279
5280 if (currentPointerCount == 1 && lastPointerCount == 1
5281 && mCurrentRawPointerData.pointers[0].toolType
5282 == mLastRawPointerData.pointers[0].toolType) {
5283 // Only one pointer and no change in count so it must have the same id as before.
5284 uint32_t id = mLastRawPointerData.pointers[0].id;
5285 mCurrentRawPointerData.pointers[0].id = id;
5286 mCurrentRawPointerData.idToIndex[id] = 0;
5287 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5288 return;
5289 }
5290
5291 // General case.
5292 // We build a heap of squared euclidean distances between current and last pointers
5293 // associated with the current and last pointer indices. Then, we find the best
5294 // match (by distance) for each current pointer.
5295 // The pointers must have the same tool type but it is possible for them to
5296 // transition from hovering to touching or vice-versa while retaining the same id.
5297 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5298
5299 uint32_t heapSize = 0;
5300 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5301 currentPointerIndex++) {
5302 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5303 lastPointerIndex++) {
5304 const RawPointerData::Pointer& currentPointer =
5305 mCurrentRawPointerData.pointers[currentPointerIndex];
5306 const RawPointerData::Pointer& lastPointer =
5307 mLastRawPointerData.pointers[lastPointerIndex];
5308 if (currentPointer.toolType == lastPointer.toolType) {
5309 int64_t deltaX = currentPointer.x - lastPointer.x;
5310 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005311
5312 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5313
5314 // Insert new element into the heap (sift up).
5315 heap[heapSize].currentPointerIndex = currentPointerIndex;
5316 heap[heapSize].lastPointerIndex = lastPointerIndex;
5317 heap[heapSize].distance = distance;
5318 heapSize += 1;
5319 }
5320 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005321 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005322
Jeff Brownbe1aa822011-07-27 16:04:54 -07005323 // Heapify
5324 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5325 startIndex -= 1;
5326 for (uint32_t parentIndex = startIndex; ;) {
5327 uint32_t childIndex = parentIndex * 2 + 1;
5328 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005329 break;
5330 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005331
5332 if (childIndex + 1 < heapSize
5333 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5334 childIndex += 1;
5335 }
5336
5337 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5338 break;
5339 }
5340
5341 swap(heap[parentIndex], heap[childIndex]);
5342 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005343 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005344 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005345
5346#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07005347 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
5348 for (size_t i = 0; i < heapSize; i++) {
5349 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5350 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5351 heap[i].distance);
5352 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005353#endif
5354
Jeff Brownbe1aa822011-07-27 16:04:54 -07005355 // Pull matches out by increasing order of distance.
5356 // To avoid reassigning pointers that have already been matched, the loop keeps track
5357 // of which last and current pointers have been matched using the matchedXXXBits variables.
5358 // It also tracks the used pointer id bits.
5359 BitSet32 matchedLastBits(0);
5360 BitSet32 matchedCurrentBits(0);
5361 BitSet32 usedIdBits(0);
5362 bool first = true;
5363 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5364 while (heapSize > 0) {
5365 if (first) {
5366 // The first time through the loop, we just consume the root element of
5367 // the heap (the one with smallest distance).
5368 first = false;
5369 } else {
5370 // Previous iterations consumed the root element of the heap.
5371 // Pop root element off of the heap (sift down).
5372 heap[0] = heap[heapSize];
5373 for (uint32_t parentIndex = 0; ;) {
5374 uint32_t childIndex = parentIndex * 2 + 1;
5375 if (childIndex >= heapSize) {
5376 break;
5377 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005378
Jeff Brownbe1aa822011-07-27 16:04:54 -07005379 if (childIndex + 1 < heapSize
5380 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5381 childIndex += 1;
5382 }
5383
5384 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5385 break;
5386 }
5387
5388 swap(heap[parentIndex], heap[childIndex]);
5389 parentIndex = childIndex;
5390 }
5391
5392#if DEBUG_POINTER_ASSIGNMENT
5393 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
5394 for (size_t i = 0; i < heapSize; i++) {
5395 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5396 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5397 heap[i].distance);
5398 }
5399#endif
5400 }
5401
5402 heapSize -= 1;
5403
5404 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5405 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5406
5407 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5408 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5409
5410 matchedCurrentBits.markBit(currentPointerIndex);
5411 matchedLastBits.markBit(lastPointerIndex);
5412
5413 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5414 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5415 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5416 mCurrentRawPointerData.markIdBit(id,
5417 mCurrentRawPointerData.isHovering(currentPointerIndex));
5418 usedIdBits.markBit(id);
5419
5420#if DEBUG_POINTER_ASSIGNMENT
5421 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
5422 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5423#endif
5424 break;
5425 }
5426 }
5427
5428 // Assign fresh ids to pointers that were not matched in the process.
5429 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5430 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5431 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5432
5433 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5434 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5435 mCurrentRawPointerData.markIdBit(id,
5436 mCurrentRawPointerData.isHovering(currentPointerIndex));
5437
5438#if DEBUG_POINTER_ASSIGNMENT
5439 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
5440 currentPointerIndex, id);
5441#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005442 }
5443}
5444
Jeff Brown6d0fec22010-07-23 21:28:06 -07005445int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005446 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5447 return AKEY_STATE_VIRTUAL;
5448 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005449
Jeff Brownbe1aa822011-07-27 16:04:54 -07005450 size_t numVirtualKeys = mVirtualKeys.size();
5451 for (size_t i = 0; i < numVirtualKeys; i++) {
5452 const VirtualKey& virtualKey = mVirtualKeys[i];
5453 if (virtualKey.keyCode == keyCode) {
5454 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005455 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005456 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005457
5458 return AKEY_STATE_UNKNOWN;
5459}
5460
5461int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005462 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5463 return AKEY_STATE_VIRTUAL;
5464 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005465
Jeff Brownbe1aa822011-07-27 16:04:54 -07005466 size_t numVirtualKeys = mVirtualKeys.size();
5467 for (size_t i = 0; i < numVirtualKeys; i++) {
5468 const VirtualKey& virtualKey = mVirtualKeys[i];
5469 if (virtualKey.scanCode == scanCode) {
5470 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005471 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005472 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005473
5474 return AKEY_STATE_UNKNOWN;
5475}
5476
5477bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5478 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005479 size_t numVirtualKeys = mVirtualKeys.size();
5480 for (size_t i = 0; i < numVirtualKeys; i++) {
5481 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005482
Jeff Brownbe1aa822011-07-27 16:04:54 -07005483 for (size_t i = 0; i < numCodes; i++) {
5484 if (virtualKey.keyCode == keyCodes[i]) {
5485 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005486 }
5487 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005488 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005489
5490 return true;
5491}
5492
5493
5494// --- SingleTouchInputMapper ---
5495
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005496SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5497 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005498}
5499
5500SingleTouchInputMapper::~SingleTouchInputMapper() {
5501}
5502
Jeff Brown65fd2512011-08-18 11:20:58 -07005503void SingleTouchInputMapper::reset(nsecs_t when) {
5504 mSingleTouchMotionAccumulator.reset(getDevice());
5505
5506 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005507}
5508
Jeff Brown6d0fec22010-07-23 21:28:06 -07005509void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005510 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005511
Jeff Brown65fd2512011-08-18 11:20:58 -07005512 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005513}
5514
Jeff Brown65fd2512011-08-18 11:20:58 -07005515void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005516 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005517 mCurrentRawPointerData.pointerCount = 1;
5518 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005519
Jeff Brown65fd2512011-08-18 11:20:58 -07005520 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5521 && (mTouchButtonAccumulator.isHovering()
5522 || (mRawPointerAxes.pressure.valid
5523 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005524 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005525
Jeff Brownbe1aa822011-07-27 16:04:54 -07005526 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005527 outPointer.id = 0;
5528 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5529 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5530 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5531 outPointer.touchMajor = 0;
5532 outPointer.touchMinor = 0;
5533 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5534 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5535 outPointer.orientation = 0;
5536 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005537 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5538 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005539 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5540 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5541 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5542 }
5543 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005544 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005545}
5546
Jeff Brownbe1aa822011-07-27 16:04:54 -07005547void SingleTouchInputMapper::configureRawPointerAxes() {
5548 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005549
Jeff Brownbe1aa822011-07-27 16:04:54 -07005550 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5551 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5552 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5553 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5554 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005555 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5556 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005557}
5558
5559
5560// --- MultiTouchInputMapper ---
5561
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005562MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005563 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005564}
5565
5566MultiTouchInputMapper::~MultiTouchInputMapper() {
5567}
5568
Jeff Brown65fd2512011-08-18 11:20:58 -07005569void MultiTouchInputMapper::reset(nsecs_t when) {
5570 mMultiTouchMotionAccumulator.reset(getDevice());
5571
Jeff Brown6894a292011-07-01 17:59:27 -07005572 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005573
Jeff Brown65fd2512011-08-18 11:20:58 -07005574 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005575}
5576
5577void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005578 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005579
Jeff Brown65fd2512011-08-18 11:20:58 -07005580 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005581}
5582
Jeff Brown65fd2512011-08-18 11:20:58 -07005583void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005584 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005585 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005586 BitSet32 newPointerIdBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005587
Jeff Brown80fd47c2011-05-24 01:07:44 -07005588 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005589 const MultiTouchMotionAccumulator::Slot* inSlot =
5590 mMultiTouchMotionAccumulator.getSlot(inIndex);
5591 if (!inSlot->isInUse()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07005592 continue;
5593 }
5594
Jeff Brown80fd47c2011-05-24 01:07:44 -07005595 if (outCount >= MAX_POINTERS) {
5596#if DEBUG_POINTERS
5597 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5598 "ignoring the rest.",
5599 getDeviceName().string(), MAX_POINTERS);
5600#endif
5601 break; // too many fingers!
5602 }
5603
Jeff Brownbe1aa822011-07-27 16:04:54 -07005604 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005605 outPointer.x = inSlot->getX();
5606 outPointer.y = inSlot->getY();
5607 outPointer.pressure = inSlot->getPressure();
5608 outPointer.touchMajor = inSlot->getTouchMajor();
5609 outPointer.touchMinor = inSlot->getTouchMinor();
5610 outPointer.toolMajor = inSlot->getToolMajor();
5611 outPointer.toolMinor = inSlot->getToolMinor();
5612 outPointer.orientation = inSlot->getOrientation();
5613 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005614 outPointer.tiltX = 0;
5615 outPointer.tiltY = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005616
Jeff Brown49754db2011-07-01 17:37:58 -07005617 outPointer.toolType = inSlot->getToolType();
5618 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5619 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5620 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5621 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5622 }
Jeff Brown8d608662010-08-30 03:02:23 -07005623 }
5624
Jeff Brown65fd2512011-08-18 11:20:58 -07005625 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5626 && (mTouchButtonAccumulator.isHovering()
5627 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005628 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005629
Jeff Brown8d608662010-08-30 03:02:23 -07005630 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005631 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005632 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005633 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005634 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005635 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005636 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005637 if (mPointerTrackingIdMap[n] == trackingId) {
5638 id = n;
5639 }
5640 }
5641
5642 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005643 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005644 mPointerTrackingIdMap[id] = trackingId;
5645 }
5646 }
5647 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005648 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005649 mCurrentRawPointerData.clearIdBits();
5650 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005651 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005652 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005653 mCurrentRawPointerData.idToIndex[id] = outCount;
5654 mCurrentRawPointerData.markIdBit(id, isHovering);
5655 newPointerIdBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005656 }
5657 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005658
Jeff Brown6d0fec22010-07-23 21:28:06 -07005659 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005660 }
5661
Jeff Brownbe1aa822011-07-27 16:04:54 -07005662 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005663 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005664
Jeff Brown65fd2512011-08-18 11:20:58 -07005665 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005666}
5667
Jeff Brownbe1aa822011-07-27 16:04:54 -07005668void MultiTouchInputMapper::configureRawPointerAxes() {
5669 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005670
Jeff Brownbe1aa822011-07-27 16:04:54 -07005671 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5672 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5673 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5674 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5675 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5676 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5677 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5678 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5679 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5680 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5681 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005682
Jeff Brownbe1aa822011-07-27 16:04:54 -07005683 if (mRawPointerAxes.trackingId.valid
5684 && mRawPointerAxes.slot.valid
5685 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5686 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005687 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005688 LOGW("MultiTouch Device %s reported %d slots but the framework "
5689 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005690 getDeviceName().string(), slotCount, MAX_SLOTS);
5691 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005692 }
Jeff Brown49754db2011-07-01 17:37:58 -07005693 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005694 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005695 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005696 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005697}
5698
Jeff Brown46b9ac02010-04-22 18:58:52 -07005699
Jeff Browncb1404e2011-01-15 18:14:15 -08005700// --- JoystickInputMapper ---
5701
5702JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5703 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005704}
5705
5706JoystickInputMapper::~JoystickInputMapper() {
5707}
5708
5709uint32_t JoystickInputMapper::getSources() {
5710 return AINPUT_SOURCE_JOYSTICK;
5711}
5712
5713void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5714 InputMapper::populateDeviceInfo(info);
5715
Jeff Brown6f2fba42011-02-19 01:08:02 -08005716 for (size_t i = 0; i < mAxes.size(); i++) {
5717 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005718 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5719 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005720 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005721 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5722 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005723 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005724 }
5725}
5726
5727void JoystickInputMapper::dump(String8& dump) {
5728 dump.append(INDENT2 "Joystick Input Mapper:\n");
5729
Jeff Brown6f2fba42011-02-19 01:08:02 -08005730 dump.append(INDENT3 "Axes:\n");
5731 size_t numAxes = mAxes.size();
5732 for (size_t i = 0; i < numAxes; i++) {
5733 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005734 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005735 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005736 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005737 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005738 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005739 }
Jeff Brown85297452011-03-04 13:07:49 -08005740 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5741 label = getAxisLabel(axis.axisInfo.highAxis);
5742 if (label) {
5743 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5744 } else {
5745 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5746 axis.axisInfo.splitValue);
5747 }
5748 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5749 dump.append(" (invert)");
5750 }
5751
5752 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5753 axis.min, axis.max, axis.flat, axis.fuzz);
5754 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5755 "highScale=%0.5f, highOffset=%0.5f\n",
5756 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005757 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5758 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005759 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005760 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005761 }
5762}
5763
Jeff Brown65fd2512011-08-18 11:20:58 -07005764void JoystickInputMapper::configure(nsecs_t when,
5765 const InputReaderConfiguration* config, uint32_t changes) {
5766 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005767
Jeff Brown474dcb52011-06-14 20:22:50 -07005768 if (!changes) { // first time only
5769 // Collect all axes.
5770 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285a2011-08-31 12:56:34 -07005771 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
5772 & INPUT_DEVICE_CLASS_JOYSTICK)) {
5773 continue; // axis must be claimed by a different device
5774 }
5775
Jeff Brown474dcb52011-06-14 20:22:50 -07005776 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005777 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005778 if (rawAxisInfo.valid) {
5779 // Map axis.
5780 AxisInfo axisInfo;
5781 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5782 if (!explicitlyMapped) {
5783 // Axis is not explicitly mapped, will choose a generic axis later.
5784 axisInfo.mode = AxisInfo::MODE_NORMAL;
5785 axisInfo.axis = -1;
5786 }
5787
5788 // Apply flat override.
5789 int32_t rawFlat = axisInfo.flatOverride < 0
5790 ? rawAxisInfo.flat : axisInfo.flatOverride;
5791
5792 // Calculate scaling factors and limits.
5793 Axis axis;
5794 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5795 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5796 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5797 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5798 scale, 0.0f, highScale, 0.0f,
5799 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5800 } else if (isCenteredAxis(axisInfo.axis)) {
5801 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5802 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5803 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5804 scale, offset, scale, offset,
5805 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5806 } else {
5807 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5808 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5809 scale, 0.0f, scale, 0.0f,
5810 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5811 }
5812
5813 // To eliminate noise while the joystick is at rest, filter out small variations
5814 // in axis values up front.
5815 axis.filter = axis.flat * 0.25f;
5816
5817 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005818 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005819 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005820
Jeff Brown474dcb52011-06-14 20:22:50 -07005821 // If there are too many axes, start dropping them.
5822 // Prefer to keep explicitly mapped axes.
5823 if (mAxes.size() > PointerCoords::MAX_AXES) {
5824 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5825 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5826 pruneAxes(true);
5827 pruneAxes(false);
5828 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005829
Jeff Brown474dcb52011-06-14 20:22:50 -07005830 // Assign generic axis ids to remaining axes.
5831 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5832 size_t numAxes = mAxes.size();
5833 for (size_t i = 0; i < numAxes; i++) {
5834 Axis& axis = mAxes.editValueAt(i);
5835 if (axis.axisInfo.axis < 0) {
5836 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5837 && haveAxis(nextGenericAxisId)) {
5838 nextGenericAxisId += 1;
5839 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005840
Jeff Brown474dcb52011-06-14 20:22:50 -07005841 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5842 axis.axisInfo.axis = nextGenericAxisId;
5843 nextGenericAxisId += 1;
5844 } else {
5845 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5846 "have already been assigned to other axes.",
5847 getDeviceName().string(), mAxes.keyAt(i));
5848 mAxes.removeItemsAt(i--);
5849 numAxes -= 1;
5850 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005851 }
5852 }
5853 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005854}
5855
Jeff Brown85297452011-03-04 13:07:49 -08005856bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005857 size_t numAxes = mAxes.size();
5858 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005859 const Axis& axis = mAxes.valueAt(i);
5860 if (axis.axisInfo.axis == axisId
5861 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5862 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005863 return true;
5864 }
5865 }
5866 return false;
5867}
Jeff Browncb1404e2011-01-15 18:14:15 -08005868
Jeff Brown6f2fba42011-02-19 01:08:02 -08005869void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5870 size_t i = mAxes.size();
5871 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5872 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5873 continue;
5874 }
5875 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5876 getDeviceName().string(), mAxes.keyAt(i));
5877 mAxes.removeItemsAt(i);
5878 }
5879}
5880
5881bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5882 switch (axis) {
5883 case AMOTION_EVENT_AXIS_X:
5884 case AMOTION_EVENT_AXIS_Y:
5885 case AMOTION_EVENT_AXIS_Z:
5886 case AMOTION_EVENT_AXIS_RX:
5887 case AMOTION_EVENT_AXIS_RY:
5888 case AMOTION_EVENT_AXIS_RZ:
5889 case AMOTION_EVENT_AXIS_HAT_X:
5890 case AMOTION_EVENT_AXIS_HAT_Y:
5891 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005892 case AMOTION_EVENT_AXIS_RUDDER:
5893 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005894 return true;
5895 default:
5896 return false;
5897 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005898}
5899
Jeff Brown65fd2512011-08-18 11:20:58 -07005900void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005901 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005902 size_t numAxes = mAxes.size();
5903 for (size_t i = 0; i < numAxes; i++) {
5904 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005905 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005906 }
5907
Jeff Brown65fd2512011-08-18 11:20:58 -07005908 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005909}
5910
5911void JoystickInputMapper::process(const RawEvent* rawEvent) {
5912 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005913 case EV_ABS: {
5914 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5915 if (index >= 0) {
5916 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005917 float newValue, highNewValue;
5918 switch (axis.axisInfo.mode) {
5919 case AxisInfo::MODE_INVERT:
5920 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5921 * axis.scale + axis.offset;
5922 highNewValue = 0.0f;
5923 break;
5924 case AxisInfo::MODE_SPLIT:
5925 if (rawEvent->value < axis.axisInfo.splitValue) {
5926 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5927 * axis.scale + axis.offset;
5928 highNewValue = 0.0f;
5929 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5930 newValue = 0.0f;
5931 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5932 * axis.highScale + axis.highOffset;
5933 } else {
5934 newValue = 0.0f;
5935 highNewValue = 0.0f;
5936 }
5937 break;
5938 default:
5939 newValue = rawEvent->value * axis.scale + axis.offset;
5940 highNewValue = 0.0f;
5941 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005942 }
Jeff Brown85297452011-03-04 13:07:49 -08005943 axis.newValue = newValue;
5944 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005945 }
5946 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005947 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005948
5949 case EV_SYN:
5950 switch (rawEvent->scanCode) {
5951 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005952 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005953 break;
5954 }
5955 break;
5956 }
5957}
5958
Jeff Brown6f2fba42011-02-19 01:08:02 -08005959void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005960 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005961 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005962 }
5963
5964 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005965 int32_t buttonState = 0;
5966
5967 PointerProperties pointerProperties;
5968 pointerProperties.clear();
5969 pointerProperties.id = 0;
5970 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005971
Jeff Brown6f2fba42011-02-19 01:08:02 -08005972 PointerCoords pointerCoords;
5973 pointerCoords.clear();
5974
5975 size_t numAxes = mAxes.size();
5976 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005977 const Axis& axis = mAxes.valueAt(i);
5978 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5979 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5980 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5981 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005982 }
5983
Jeff Brown56194eb2011-03-02 19:23:13 -08005984 // Moving a joystick axis should not wake the devide because joysticks can
5985 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5986 // button will likely wake the device.
5987 // TODO: Use the input device configuration to control this behavior more finely.
5988 uint32_t policyFlags = 0;
5989
Jeff Brownbe1aa822011-07-27 16:04:54 -07005990 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005991 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5992 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005993 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005994}
5995
Jeff Brown85297452011-03-04 13:07:49 -08005996bool JoystickInputMapper::filterAxes(bool force) {
5997 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005998 size_t numAxes = mAxes.size();
5999 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006000 Axis& axis = mAxes.editValueAt(i);
6001 if (force || hasValueChangedSignificantly(axis.filter,
6002 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6003 axis.currentValue = axis.newValue;
6004 atLeastOneSignificantChange = true;
6005 }
6006 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6007 if (force || hasValueChangedSignificantly(axis.filter,
6008 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6009 axis.highCurrentValue = axis.highNewValue;
6010 atLeastOneSignificantChange = true;
6011 }
6012 }
6013 }
6014 return atLeastOneSignificantChange;
6015}
6016
6017bool JoystickInputMapper::hasValueChangedSignificantly(
6018 float filter, float newValue, float currentValue, float min, float max) {
6019 if (newValue != currentValue) {
6020 // Filter out small changes in value unless the value is converging on the axis
6021 // bounds or center point. This is intended to reduce the amount of information
6022 // sent to applications by particularly noisy joysticks (such as PS3).
6023 if (fabs(newValue - currentValue) > filter
6024 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6025 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6026 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6027 return true;
6028 }
6029 }
6030 return false;
6031}
6032
6033bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6034 float filter, float newValue, float currentValue, float thresholdValue) {
6035 float newDistance = fabs(newValue - thresholdValue);
6036 if (newDistance < filter) {
6037 float oldDistance = fabs(currentValue - thresholdValue);
6038 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006039 return true;
6040 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006041 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006042 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006043}
6044
Jeff Brown46b9ac02010-04-22 18:58:52 -07006045} // namespace android