blob: bfcf8e048111ffff86abdf40e27d661d35b6e7a6 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070041#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080043#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
Jeff Brown60691392011-07-15 19:08:26 -0700123static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800124 return rotateValueUsingRotationMap(keyCode, orientation,
125 keyCodeRotationMap, keyCodeRotationMapSize);
126}
127
Jeff Brown612891e2011-07-15 20:44:17 -0700128static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
129 float temp;
130 switch (orientation) {
131 case DISPLAY_ORIENTATION_90:
132 temp = *deltaX;
133 *deltaX = *deltaY;
134 *deltaY = -temp;
135 break;
136
137 case DISPLAY_ORIENTATION_180:
138 *deltaX = -*deltaX;
139 *deltaY = -*deltaY;
140 break;
141
142 case DISPLAY_ORIENTATION_270:
143 temp = *deltaX;
144 *deltaX = -*deltaY;
145 *deltaY = temp;
146 break;
147 }
148}
149
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
151 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
152}
153
Jeff Brownefd32662011-03-08 15:13:06 -0800154// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700155// button states. This determines whether the event is reported as a touch event.
156static bool isPointerDown(int32_t buttonState) {
157 return buttonState &
158 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700159 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800160}
161
Jeff Brown2352b972011-04-12 22:39:53 -0700162static float calculateCommonVector(float a, float b) {
163 if (a > 0 && b > 0) {
164 return a < b ? a : b;
165 } else if (a < 0 && b < 0) {
166 return a > b ? a : b;
167 } else {
168 return 0;
169 }
170}
171
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700172static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
173 nsecs_t when, int32_t deviceId, uint32_t source,
174 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
175 int32_t buttonState, int32_t keyCode) {
176 if (
177 (action == AKEY_EVENT_ACTION_DOWN
178 && !(lastButtonState & buttonState)
179 && (currentButtonState & buttonState))
180 || (action == AKEY_EVENT_ACTION_UP
181 && (lastButtonState & buttonState)
182 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700183 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700184 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700185 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700186 }
187}
188
189static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
190 nsecs_t when, int32_t deviceId, uint32_t source,
191 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
192 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
193 lastButtonState, currentButtonState,
194 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
198}
199
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700200
Jeff Brown65fd2512011-08-18 11:20:58 -0700201// --- InputReaderConfiguration ---
202
203bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external,
204 int32_t* width, int32_t* height, int32_t* orientation) const {
205 if (displayId == 0) {
206 const DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
207 if (info.width > 0 && info.height > 0) {
208 if (width) {
209 *width = info.width;
210 }
211 if (height) {
212 *height = info.height;
213 }
214 if (orientation) {
215 *orientation = info.orientation;
216 }
217 return true;
218 }
219 }
220 return false;
221}
222
223void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,
224 int32_t width, int32_t height, int32_t orientation) {
225 if (displayId == 0) {
226 DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
227 info.width = width;
228 info.height = height;
229 info.orientation = orientation;
230 }
231}
232
233
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700234// --- InputReader ---
235
236InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700238 const sp<InputListenerInterface>& listener) :
239 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700240 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700241 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700242 mQueuedListener = new QueuedInputListener(listener);
243
244 { // acquire lock
245 AutoMutex _l(mLock);
246
247 refreshConfigurationLocked(0);
248 updateGlobalMetaStateLocked();
249 updateInputConfigurationLocked();
250 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700251}
252
253InputReader::~InputReader() {
254 for (size_t i = 0; i < mDevices.size(); i++) {
255 delete mDevices.valueAt(i);
256 }
257}
258
259void InputReader::loopOnce() {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700260 int32_t timeoutMillis;
Jeff Brown474dcb52011-06-14 20:22:50 -0700261 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700262 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700263
Jeff Brownbe1aa822011-07-27 16:04:54 -0700264 uint32_t changes = mConfigurationChangesToRefresh;
265 if (changes) {
266 mConfigurationChangesToRefresh = 0;
267 refreshConfigurationLocked(changes);
268 }
269
270 timeoutMillis = -1;
271 if (mNextTimeout != LLONG_MAX) {
272 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
273 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
274 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700275 } // release lock
276
Jeff Brownb7198742011-03-18 18:14:26 -0700277 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700278
279 { // acquire lock
280 AutoMutex _l(mLock);
281
282 if (count) {
283 processEventsLocked(mEventBuffer, count);
284 }
285 if (!count || timeoutMillis == 0) {
286 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700287#if DEBUG_RAW_EVENTS
Jeff Brownbe1aa822011-07-27 16:04:54 -0700288 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700289#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700290 mNextTimeout = LLONG_MAX;
291 timeoutExpiredLocked(now);
292 }
293 } // release lock
294
295 // Flush queued events out to the listener.
296 // This must happen outside of the lock because the listener could potentially call
297 // back into the InputReader's methods, such as getScanCodeState, or become blocked
298 // on another thread similarly waiting to acquire the InputReader lock thereby
299 // resulting in a deadlock. This situation is actually quite plausible because the
300 // listener is actually the input dispatcher, which calls into the window manager,
301 // which occasionally calls into the input reader.
302 mQueuedListener->flush();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700303}
304
Jeff Brownbe1aa822011-07-27 16:04:54 -0700305void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700306 for (const RawEvent* rawEvent = rawEvents; count;) {
307 int32_t type = rawEvent->type;
308 size_t batchSize = 1;
309 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
310 int32_t deviceId = rawEvent->deviceId;
311 while (batchSize < count) {
312 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
313 || rawEvent[batchSize].deviceId != deviceId) {
314 break;
315 }
316 batchSize += 1;
317 }
318#if DEBUG_RAW_EVENTS
319 LOGD("BatchSize: %d Count: %d", batchSize, count);
320#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700321 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700322 } else {
323 switch (rawEvent->type) {
324 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700325 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700326 break;
327 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700328 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700329 break;
330 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700331 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700332 break;
333 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700334 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700335 break;
336 }
337 }
338 count -= batchSize;
339 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700340 }
341}
342
Jeff Brown65fd2512011-08-18 11:20:58 -0700343void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700344 String8 name = mEventHub->getDeviceName(deviceId);
345 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
346
Jeff Brownbe1aa822011-07-27 16:04:54 -0700347 InputDevice* device = createDeviceLocked(deviceId, name, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700348 device->configure(when, &mConfig, 0);
349 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700350
Jeff Brown8d608662010-08-30 03:02:23 -0700351 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800352 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700353 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800354 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700355 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700356 }
357
Jeff Brownbe1aa822011-07-27 16:04:54 -0700358 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
359 if (deviceIndex < 0) {
360 mDevices.add(deviceId, device);
361 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700362 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
363 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700364 return;
365 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700366}
367
Jeff Brown65fd2512011-08-18 11:20:58 -0700368void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700369 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700370 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
371 if (deviceIndex >= 0) {
372 device = mDevices.valueAt(deviceIndex);
373 mDevices.removeItemsAt(deviceIndex, 1);
374 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700375 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700376 return;
377 }
378
Jeff Brown6d0fec22010-07-23 21:28:06 -0700379 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800380 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700381 device->getId(), device->getName().string());
382 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800383 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700384 device->getId(), device->getName().string(), device->getSources());
385 }
386
Jeff Brown65fd2512011-08-18 11:20:58 -0700387 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700388 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700389}
390
Jeff Brownbe1aa822011-07-27 16:04:54 -0700391InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
392 const String8& name, uint32_t classes) {
393 InputDevice* device = new InputDevice(&mContext, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700394
Jeff Brown56194eb2011-03-02 19:23:13 -0800395 // External devices.
396 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
397 device->setExternal(true);
398 }
399
Jeff Brown6d0fec22010-07-23 21:28:06 -0700400 // Switch-like devices.
401 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
402 device->addMapper(new SwitchInputMapper(device));
403 }
404
405 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800406 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
408 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800409 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700410 }
411 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
412 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
413 }
414 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800415 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700416 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800417 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800418 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800419 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700420
Jeff Brownefd32662011-03-08 15:13:06 -0800421 if (keyboardSource != 0) {
422 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700423 }
424
Jeff Brown83c09682010-12-23 17:50:18 -0800425 // Cursor-like devices.
426 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
427 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700428 }
429
Jeff Brown58a2da82011-01-25 16:02:22 -0800430 // Touchscreens and touchpad devices.
431 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800432 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800433 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800434 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435 }
436
Jeff Browncb1404e2011-01-15 18:14:15 -0800437 // Joystick-like devices.
438 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
439 device->addMapper(new JoystickInputMapper(device));
440 }
441
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442 return device;
443}
444
Jeff Brownbe1aa822011-07-27 16:04:54 -0700445void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700446 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700447 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
448 if (deviceIndex < 0) {
449 LOGW("Discarding event for unknown deviceId %d.", deviceId);
450 return;
451 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700452
Jeff Brownbe1aa822011-07-27 16:04:54 -0700453 InputDevice* device = mDevices.valueAt(deviceIndex);
454 if (device->isIgnored()) {
455 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
456 return;
457 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700458
Jeff Brownbe1aa822011-07-27 16:04:54 -0700459 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700460}
461
Jeff Brownbe1aa822011-07-27 16:04:54 -0700462void InputReader::timeoutExpiredLocked(nsecs_t when) {
463 for (size_t i = 0; i < mDevices.size(); i++) {
464 InputDevice* device = mDevices.valueAt(i);
465 if (!device->isIgnored()) {
466 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700467 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700468 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700469}
470
Jeff Brownbe1aa822011-07-27 16:04:54 -0700471void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700473 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700474
475 // Update input configuration.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700476 updateInputConfigurationLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700477
478 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700479 NotifyConfigurationChangedArgs args(when);
480 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700481}
482
Jeff Brownbe1aa822011-07-27 16:04:54 -0700483void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700484 mPolicy->getReaderConfiguration(&mConfig);
485 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
486
Jeff Brown474dcb52011-06-14 20:22:50 -0700487 if (changes) {
488 LOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700489 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700490
491 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
492 mEventHub->requestReopenDevices();
493 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700494 for (size_t i = 0; i < mDevices.size(); i++) {
495 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700496 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700497 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700498 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700499 }
500}
501
Jeff Brownbe1aa822011-07-27 16:04:54 -0700502void InputReader::updateGlobalMetaStateLocked() {
503 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504
Jeff Brownbe1aa822011-07-27 16:04:54 -0700505 for (size_t i = 0; i < mDevices.size(); i++) {
506 InputDevice* device = mDevices.valueAt(i);
507 mGlobalMetaState |= device->getMetaState();
508 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700509}
510
Jeff Brownbe1aa822011-07-27 16:04:54 -0700511int32_t InputReader::getGlobalMetaStateLocked() {
512 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700513}
514
Jeff Brownbe1aa822011-07-27 16:04:54 -0700515void InputReader::updateInputConfigurationLocked() {
516 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
517 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
518 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
519 InputDeviceInfo deviceInfo;
520 for (size_t i = 0; i < mDevices.size(); i++) {
521 InputDevice* device = mDevices.valueAt(i);
522 device->getDeviceInfo(& deviceInfo);
523 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700524
Jeff Brownbe1aa822011-07-27 16:04:54 -0700525 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
526 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
527 }
528 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
529 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
530 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
531 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
532 }
533 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
534 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
535 }
536 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700537
Jeff Brownbe1aa822011-07-27 16:04:54 -0700538 mInputConfiguration.touchScreen = touchScreenConfig;
539 mInputConfiguration.keyboard = keyboardConfig;
540 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700541}
542
Jeff Brownbe1aa822011-07-27 16:04:54 -0700543void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800544 mDisableVirtualKeysTimeout = time;
545}
546
Jeff Brownbe1aa822011-07-27 16:04:54 -0700547bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800548 InputDevice* device, int32_t keyCode, int32_t scanCode) {
549 if (now < mDisableVirtualKeysTimeout) {
550 LOGI("Dropping virtual key from device %s because virtual keys are "
551 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
552 device->getName().string(),
553 (mDisableVirtualKeysTimeout - now) * 0.000001,
554 keyCode, scanCode);
555 return true;
556 } else {
557 return false;
558 }
559}
560
Jeff Brownbe1aa822011-07-27 16:04:54 -0700561void InputReader::fadePointerLocked() {
562 for (size_t i = 0; i < mDevices.size(); i++) {
563 InputDevice* device = mDevices.valueAt(i);
564 device->fadePointer();
565 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800566}
567
Jeff Brownbe1aa822011-07-27 16:04:54 -0700568void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700569 if (when < mNextTimeout) {
570 mNextTimeout = when;
571 }
572}
573
Jeff Brown6d0fec22010-07-23 21:28:06 -0700574void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700575 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700576
Jeff Brownbe1aa822011-07-27 16:04:54 -0700577 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700578}
579
580status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700581 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700582
Jeff Brownbe1aa822011-07-27 16:04:54 -0700583 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
584 if (deviceIndex < 0) {
585 return NAME_NOT_FOUND;
586 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700587
Jeff Brownbe1aa822011-07-27 16:04:54 -0700588 InputDevice* device = mDevices.valueAt(deviceIndex);
589 if (device->isIgnored()) {
590 return NAME_NOT_FOUND;
591 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700592
Jeff Brownbe1aa822011-07-27 16:04:54 -0700593 device->getDeviceInfo(outDeviceInfo);
594 return OK;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700595}
596
597void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700598 AutoMutex _l(mLock);
599
Jeff Brown6d0fec22010-07-23 21:28:06 -0700600 outDeviceIds.clear();
601
Jeff Brownbe1aa822011-07-27 16:04:54 -0700602 size_t numDevices = mDevices.size();
603 for (size_t i = 0; i < numDevices; i++) {
604 InputDevice* device = mDevices.valueAt(i);
605 if (!device->isIgnored()) {
606 outDeviceIds.add(device->getId());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700607 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700608 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700609}
610
611int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
612 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700613 AutoMutex _l(mLock);
614
615 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700616}
617
618int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
619 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700620 AutoMutex _l(mLock);
621
622 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700623}
624
625int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700626 AutoMutex _l(mLock);
627
628 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700629}
630
Jeff Brownbe1aa822011-07-27 16:04:54 -0700631int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700632 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700633 int32_t result = AKEY_STATE_UNKNOWN;
634 if (deviceId >= 0) {
635 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
636 if (deviceIndex >= 0) {
637 InputDevice* device = mDevices.valueAt(deviceIndex);
638 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
639 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700640 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700641 }
642 } else {
643 size_t numDevices = mDevices.size();
644 for (size_t i = 0; i < numDevices; i++) {
645 InputDevice* device = mDevices.valueAt(i);
646 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
647 result = (device->*getStateFunc)(sourceMask, code);
648 if (result >= AKEY_STATE_DOWN) {
649 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700650 }
651 }
652 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700653 }
654 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700655}
656
657bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
658 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700659 AutoMutex _l(mLock);
660
Jeff Brown6d0fec22010-07-23 21:28:06 -0700661 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700662 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700663}
664
Jeff Brownbe1aa822011-07-27 16:04:54 -0700665bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
666 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
667 bool result = false;
668 if (deviceId >= 0) {
669 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
670 if (deviceIndex >= 0) {
671 InputDevice* device = mDevices.valueAt(deviceIndex);
672 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
673 result = device->markSupportedKeyCodes(sourceMask,
674 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700675 }
676 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700677 } else {
678 size_t numDevices = mDevices.size();
679 for (size_t i = 0; i < numDevices; i++) {
680 InputDevice* device = mDevices.valueAt(i);
681 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
682 result |= device->markSupportedKeyCodes(sourceMask,
683 numCodes, keyCodes, outFlags);
684 }
685 }
686 }
687 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700688}
689
Jeff Brown474dcb52011-06-14 20:22:50 -0700690void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700691 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700692
Jeff Brownbe1aa822011-07-27 16:04:54 -0700693 if (changes) {
694 bool needWake = !mConfigurationChangesToRefresh;
695 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700696
697 if (needWake) {
698 mEventHub->wake();
699 }
700 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700701}
702
Jeff Brownb88102f2010-09-08 11:49:43 -0700703void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700704 AutoMutex _l(mLock);
705
Jeff Brownf2f487182010-10-01 17:46:21 -0700706 mEventHub->dump(dump);
707 dump.append("\n");
708
709 dump.append("Input Reader State:\n");
710
Jeff Brownbe1aa822011-07-27 16:04:54 -0700711 for (size_t i = 0; i < mDevices.size(); i++) {
712 mDevices.valueAt(i)->dump(dump);
713 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700714
715 dump.append(INDENT "Configuration:\n");
716 dump.append(INDENT2 "ExcludedDeviceNames: [");
717 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
718 if (i != 0) {
719 dump.append(", ");
720 }
721 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
722 }
723 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700724 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
725 mConfig.virtualKeyQuietTime * 0.000001f);
726
Jeff Brown19c97d462011-06-01 12:33:19 -0700727 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
728 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
729 mConfig.pointerVelocityControlParameters.scale,
730 mConfig.pointerVelocityControlParameters.lowThreshold,
731 mConfig.pointerVelocityControlParameters.highThreshold,
732 mConfig.pointerVelocityControlParameters.acceleration);
733
734 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
735 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
736 mConfig.wheelVelocityControlParameters.scale,
737 mConfig.wheelVelocityControlParameters.lowThreshold,
738 mConfig.wheelVelocityControlParameters.highThreshold,
739 mConfig.wheelVelocityControlParameters.acceleration);
740
Jeff Brown214eaf42011-05-26 19:17:02 -0700741 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700742 dump.appendFormat(INDENT3 "Enabled: %s\n",
743 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700744 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
745 mConfig.pointerGestureQuietInterval * 0.000001f);
746 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
747 mConfig.pointerGestureDragMinSwitchSpeed);
748 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
749 mConfig.pointerGestureTapInterval * 0.000001f);
750 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
751 mConfig.pointerGestureTapDragInterval * 0.000001f);
752 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
753 mConfig.pointerGestureTapSlop);
754 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
755 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700756 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
757 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700758 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
759 mConfig.pointerGestureSwipeTransitionAngleCosine);
760 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
761 mConfig.pointerGestureSwipeMaxWidthRatio);
762 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
763 mConfig.pointerGestureMovementSpeedRatio);
764 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
765 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700766}
767
Jeff Brown89ef0722011-08-10 16:25:21 -0700768void InputReader::monitor() {
769 // Acquire and release the lock to ensure that the reader has not deadlocked.
770 mLock.lock();
771 mLock.unlock();
772
773 // Check the EventHub
774 mEventHub->monitor();
775}
776
Jeff Brown6d0fec22010-07-23 21:28:06 -0700777
Jeff Brownbe1aa822011-07-27 16:04:54 -0700778// --- InputReader::ContextImpl ---
779
780InputReader::ContextImpl::ContextImpl(InputReader* reader) :
781 mReader(reader) {
782}
783
784void InputReader::ContextImpl::updateGlobalMetaState() {
785 // lock is already held by the input loop
786 mReader->updateGlobalMetaStateLocked();
787}
788
789int32_t InputReader::ContextImpl::getGlobalMetaState() {
790 // lock is already held by the input loop
791 return mReader->getGlobalMetaStateLocked();
792}
793
794void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
795 // lock is already held by the input loop
796 mReader->disableVirtualKeysUntilLocked(time);
797}
798
799bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
800 InputDevice* device, int32_t keyCode, int32_t scanCode) {
801 // lock is already held by the input loop
802 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
803}
804
805void InputReader::ContextImpl::fadePointer() {
806 // lock is already held by the input loop
807 mReader->fadePointerLocked();
808}
809
810void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
811 // lock is already held by the input loop
812 mReader->requestTimeoutAtTimeLocked(when);
813}
814
815InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
816 return mReader->mPolicy.get();
817}
818
819InputListenerInterface* InputReader::ContextImpl::getListener() {
820 return mReader->mQueuedListener.get();
821}
822
823EventHubInterface* InputReader::ContextImpl::getEventHub() {
824 return mReader->mEventHub.get();
825}
826
827
Jeff Brown6d0fec22010-07-23 21:28:06 -0700828// --- InputReaderThread ---
829
830InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
831 Thread(/*canCallJava*/ true), mReader(reader) {
832}
833
834InputReaderThread::~InputReaderThread() {
835}
836
837bool InputReaderThread::threadLoop() {
838 mReader->loopOnce();
839 return true;
840}
841
842
843// --- InputDevice ---
844
845InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown80fd47c2011-05-24 01:07:44 -0700846 mContext(context), mId(id), mName(name), mSources(0),
847 mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700848}
849
850InputDevice::~InputDevice() {
851 size_t numMappers = mMappers.size();
852 for (size_t i = 0; i < numMappers; i++) {
853 delete mMappers[i];
854 }
855 mMappers.clear();
856}
857
Jeff Brownef3d7e82010-09-30 14:33:04 -0700858void InputDevice::dump(String8& dump) {
859 InputDeviceInfo deviceInfo;
860 getDeviceInfo(& deviceInfo);
861
Jeff Brown90655042010-12-02 13:50:46 -0800862 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700863 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800864 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700865 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
866 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800867
Jeff Brownefd32662011-03-08 15:13:06 -0800868 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800869 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700870 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800871 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800872 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
873 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800874 char name[32];
875 if (label) {
876 strncpy(name, label, sizeof(name));
877 name[sizeof(name) - 1] = '\0';
878 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800879 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800880 }
Jeff Brownefd32662011-03-08 15:13:06 -0800881 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
882 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
883 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800884 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700885 }
886
887 size_t numMappers = mMappers.size();
888 for (size_t i = 0; i < numMappers; i++) {
889 InputMapper* mapper = mMappers[i];
890 mapper->dump(dump);
891 }
892}
893
Jeff Brown6d0fec22010-07-23 21:28:06 -0700894void InputDevice::addMapper(InputMapper* mapper) {
895 mMappers.add(mapper);
896}
897
Jeff Brown65fd2512011-08-18 11:20:58 -0700898void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700899 mSources = 0;
900
Jeff Brown474dcb52011-06-14 20:22:50 -0700901 if (!isIgnored()) {
902 if (!changes) { // first time only
903 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
904 }
905
906 size_t numMappers = mMappers.size();
907 for (size_t i = 0; i < numMappers; i++) {
908 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700909 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700910 mSources |= mapper->getSources();
911 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700912 }
913}
914
Jeff Brown65fd2512011-08-18 11:20:58 -0700915void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700916 size_t numMappers = mMappers.size();
917 for (size_t i = 0; i < numMappers; i++) {
918 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700919 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700920 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700921
922 mContext->updateGlobalMetaState();
923
924 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700925}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700926
Jeff Brownb7198742011-03-18 18:14:26 -0700927void InputDevice::process(const RawEvent* rawEvents, size_t count) {
928 // Process all of the events in order for each mapper.
929 // We cannot simply ask each mapper to process them in bulk because mappers may
930 // have side-effects that must be interleaved. For example, joystick movement events and
931 // gamepad button presses are handled by different mappers but they should be dispatched
932 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700933 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700934 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
935#if DEBUG_RAW_EVENTS
936 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700937 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700938 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
939 rawEvent->value, rawEvent->flags);
940#endif
941
Jeff Brown80fd47c2011-05-24 01:07:44 -0700942 if (mDropUntilNextSync) {
943 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
944 mDropUntilNextSync = false;
945#if DEBUG_RAW_EVENTS
946 LOGD("Recovered from input event buffer overrun.");
947#endif
948 } else {
949#if DEBUG_RAW_EVENTS
950 LOGD("Dropped input event while waiting for next input sync.");
951#endif
952 }
953 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
954 LOGI("Detected input event buffer overrun for device %s.", mName.string());
955 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700956 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700957 } else {
958 for (size_t i = 0; i < numMappers; i++) {
959 InputMapper* mapper = mMappers[i];
960 mapper->process(rawEvent);
961 }
Jeff Brownb7198742011-03-18 18:14:26 -0700962 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700963 }
964}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700965
Jeff Brownaa3855d2011-03-17 01:34:19 -0700966void InputDevice::timeoutExpired(nsecs_t when) {
967 size_t numMappers = mMappers.size();
968 for (size_t i = 0; i < numMappers; i++) {
969 InputMapper* mapper = mMappers[i];
970 mapper->timeoutExpired(when);
971 }
972}
973
Jeff Brown6d0fec22010-07-23 21:28:06 -0700974void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
975 outDeviceInfo->initialize(mId, mName);
976
977 size_t numMappers = mMappers.size();
978 for (size_t i = 0; i < numMappers; i++) {
979 InputMapper* mapper = mMappers[i];
980 mapper->populateDeviceInfo(outDeviceInfo);
981 }
982}
983
984int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
985 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
986}
987
988int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
989 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
990}
991
992int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
993 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
994}
995
996int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
997 int32_t result = AKEY_STATE_UNKNOWN;
998 size_t numMappers = mMappers.size();
999 for (size_t i = 0; i < numMappers; i++) {
1000 InputMapper* mapper = mMappers[i];
1001 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1002 result = (mapper->*getStateFunc)(sourceMask, code);
1003 if (result >= AKEY_STATE_DOWN) {
1004 return result;
1005 }
1006 }
1007 }
1008 return result;
1009}
1010
1011bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1012 const int32_t* keyCodes, uint8_t* outFlags) {
1013 bool result = false;
1014 size_t numMappers = mMappers.size();
1015 for (size_t i = 0; i < numMappers; i++) {
1016 InputMapper* mapper = mMappers[i];
1017 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1018 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1019 }
1020 }
1021 return result;
1022}
1023
1024int32_t InputDevice::getMetaState() {
1025 int32_t result = 0;
1026 size_t numMappers = mMappers.size();
1027 for (size_t i = 0; i < numMappers; i++) {
1028 InputMapper* mapper = mMappers[i];
1029 result |= mapper->getMetaState();
1030 }
1031 return result;
1032}
1033
Jeff Brown05dc66a2011-03-02 14:41:58 -08001034void InputDevice::fadePointer() {
1035 size_t numMappers = mMappers.size();
1036 for (size_t i = 0; i < numMappers; i++) {
1037 InputMapper* mapper = mMappers[i];
1038 mapper->fadePointer();
1039 }
1040}
1041
Jeff Brown65fd2512011-08-18 11:20:58 -07001042void InputDevice::notifyReset(nsecs_t when) {
1043 NotifyDeviceResetArgs args(when, mId);
1044 mContext->getListener()->notifyDeviceReset(&args);
1045}
1046
Jeff Brown6d0fec22010-07-23 21:28:06 -07001047
Jeff Brown49754db2011-07-01 17:37:58 -07001048// --- CursorButtonAccumulator ---
1049
1050CursorButtonAccumulator::CursorButtonAccumulator() {
1051 clearButtons();
1052}
1053
Jeff Brown65fd2512011-08-18 11:20:58 -07001054void CursorButtonAccumulator::reset(InputDevice* device) {
1055 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1056 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1057 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1058 mBtnBack = device->isKeyPressed(BTN_BACK);
1059 mBtnSide = device->isKeyPressed(BTN_SIDE);
1060 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1061 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1062 mBtnTask = device->isKeyPressed(BTN_TASK);
1063}
1064
Jeff Brown49754db2011-07-01 17:37:58 -07001065void CursorButtonAccumulator::clearButtons() {
1066 mBtnLeft = 0;
1067 mBtnRight = 0;
1068 mBtnMiddle = 0;
1069 mBtnBack = 0;
1070 mBtnSide = 0;
1071 mBtnForward = 0;
1072 mBtnExtra = 0;
1073 mBtnTask = 0;
1074}
1075
1076void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1077 if (rawEvent->type == EV_KEY) {
1078 switch (rawEvent->scanCode) {
1079 case BTN_LEFT:
1080 mBtnLeft = rawEvent->value;
1081 break;
1082 case BTN_RIGHT:
1083 mBtnRight = rawEvent->value;
1084 break;
1085 case BTN_MIDDLE:
1086 mBtnMiddle = rawEvent->value;
1087 break;
1088 case BTN_BACK:
1089 mBtnBack = rawEvent->value;
1090 break;
1091 case BTN_SIDE:
1092 mBtnSide = rawEvent->value;
1093 break;
1094 case BTN_FORWARD:
1095 mBtnForward = rawEvent->value;
1096 break;
1097 case BTN_EXTRA:
1098 mBtnExtra = rawEvent->value;
1099 break;
1100 case BTN_TASK:
1101 mBtnTask = rawEvent->value;
1102 break;
1103 }
1104 }
1105}
1106
1107uint32_t CursorButtonAccumulator::getButtonState() const {
1108 uint32_t result = 0;
1109 if (mBtnLeft) {
1110 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1111 }
1112 if (mBtnRight) {
1113 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1114 }
1115 if (mBtnMiddle) {
1116 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1117 }
1118 if (mBtnBack || mBtnSide) {
1119 result |= AMOTION_EVENT_BUTTON_BACK;
1120 }
1121 if (mBtnForward || mBtnExtra) {
1122 result |= AMOTION_EVENT_BUTTON_FORWARD;
1123 }
1124 return result;
1125}
1126
1127
1128// --- CursorMotionAccumulator ---
1129
Jeff Brown65fd2512011-08-18 11:20:58 -07001130CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001131 clearRelativeAxes();
1132}
1133
Jeff Brown65fd2512011-08-18 11:20:58 -07001134void CursorMotionAccumulator::reset(InputDevice* device) {
1135 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001136}
1137
1138void CursorMotionAccumulator::clearRelativeAxes() {
1139 mRelX = 0;
1140 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001141}
1142
1143void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1144 if (rawEvent->type == EV_REL) {
1145 switch (rawEvent->scanCode) {
1146 case REL_X:
1147 mRelX = rawEvent->value;
1148 break;
1149 case REL_Y:
1150 mRelY = rawEvent->value;
1151 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001152 }
1153 }
1154}
1155
1156void CursorMotionAccumulator::finishSync() {
1157 clearRelativeAxes();
1158}
1159
1160
1161// --- CursorScrollAccumulator ---
1162
1163CursorScrollAccumulator::CursorScrollAccumulator() :
1164 mHaveRelWheel(false), mHaveRelHWheel(false) {
1165 clearRelativeAxes();
1166}
1167
1168void CursorScrollAccumulator::configure(InputDevice* device) {
1169 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1170 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1171}
1172
1173void CursorScrollAccumulator::reset(InputDevice* device) {
1174 clearRelativeAxes();
1175}
1176
1177void CursorScrollAccumulator::clearRelativeAxes() {
1178 mRelWheel = 0;
1179 mRelHWheel = 0;
1180}
1181
1182void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1183 if (rawEvent->type == EV_REL) {
1184 switch (rawEvent->scanCode) {
Jeff Brown49754db2011-07-01 17:37:58 -07001185 case REL_WHEEL:
1186 mRelWheel = rawEvent->value;
1187 break;
1188 case REL_HWHEEL:
1189 mRelHWheel = rawEvent->value;
1190 break;
1191 }
1192 }
1193}
1194
Jeff Brown65fd2512011-08-18 11:20:58 -07001195void CursorScrollAccumulator::finishSync() {
1196 clearRelativeAxes();
1197}
1198
Jeff Brown49754db2011-07-01 17:37:58 -07001199
1200// --- TouchButtonAccumulator ---
1201
1202TouchButtonAccumulator::TouchButtonAccumulator() :
1203 mHaveBtnTouch(false) {
1204 clearButtons();
1205}
1206
1207void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001208 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1209}
1210
1211void TouchButtonAccumulator::reset(InputDevice* device) {
1212 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1213 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1214 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1215 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1216 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1217 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1218 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1219 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1220 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1221 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1222 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001223 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1224 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1225 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001226}
1227
1228void TouchButtonAccumulator::clearButtons() {
1229 mBtnTouch = 0;
1230 mBtnStylus = 0;
1231 mBtnStylus2 = 0;
1232 mBtnToolFinger = 0;
1233 mBtnToolPen = 0;
1234 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001235 mBtnToolBrush = 0;
1236 mBtnToolPencil = 0;
1237 mBtnToolAirbrush = 0;
1238 mBtnToolMouse = 0;
1239 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001240 mBtnToolDoubleTap = 0;
1241 mBtnToolTripleTap = 0;
1242 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001243}
1244
1245void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1246 if (rawEvent->type == EV_KEY) {
1247 switch (rawEvent->scanCode) {
1248 case BTN_TOUCH:
1249 mBtnTouch = rawEvent->value;
1250 break;
1251 case BTN_STYLUS:
1252 mBtnStylus = rawEvent->value;
1253 break;
1254 case BTN_STYLUS2:
1255 mBtnStylus2 = rawEvent->value;
1256 break;
1257 case BTN_TOOL_FINGER:
1258 mBtnToolFinger = rawEvent->value;
1259 break;
1260 case BTN_TOOL_PEN:
1261 mBtnToolPen = rawEvent->value;
1262 break;
1263 case BTN_TOOL_RUBBER:
1264 mBtnToolRubber = rawEvent->value;
1265 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001266 case BTN_TOOL_BRUSH:
1267 mBtnToolBrush = rawEvent->value;
1268 break;
1269 case BTN_TOOL_PENCIL:
1270 mBtnToolPencil = rawEvent->value;
1271 break;
1272 case BTN_TOOL_AIRBRUSH:
1273 mBtnToolAirbrush = rawEvent->value;
1274 break;
1275 case BTN_TOOL_MOUSE:
1276 mBtnToolMouse = rawEvent->value;
1277 break;
1278 case BTN_TOOL_LENS:
1279 mBtnToolLens = rawEvent->value;
1280 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001281 case BTN_TOOL_DOUBLETAP:
1282 mBtnToolDoubleTap = rawEvent->value;
1283 break;
1284 case BTN_TOOL_TRIPLETAP:
1285 mBtnToolTripleTap = rawEvent->value;
1286 break;
1287 case BTN_TOOL_QUADTAP:
1288 mBtnToolQuadTap = rawEvent->value;
1289 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001290 }
1291 }
1292}
1293
1294uint32_t TouchButtonAccumulator::getButtonState() const {
1295 uint32_t result = 0;
1296 if (mBtnStylus) {
1297 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1298 }
1299 if (mBtnStylus2) {
1300 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1301 }
1302 return result;
1303}
1304
1305int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001306 if (mBtnToolMouse || mBtnToolLens) {
1307 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1308 }
Jeff Brown49754db2011-07-01 17:37:58 -07001309 if (mBtnToolRubber) {
1310 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1311 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001312 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001313 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1314 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001315 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001316 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1317 }
1318 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1319}
1320
Jeff Brownd87c6d52011-08-10 14:55:59 -07001321bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001322 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1323 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001324 || mBtnToolMouse || mBtnToolLens
1325 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001326}
1327
1328bool TouchButtonAccumulator::isHovering() const {
1329 return mHaveBtnTouch && !mBtnTouch;
1330}
1331
1332
Jeff Brownbe1aa822011-07-27 16:04:54 -07001333// --- RawPointerAxes ---
1334
1335RawPointerAxes::RawPointerAxes() {
1336 clear();
1337}
1338
1339void RawPointerAxes::clear() {
1340 x.clear();
1341 y.clear();
1342 pressure.clear();
1343 touchMajor.clear();
1344 touchMinor.clear();
1345 toolMajor.clear();
1346 toolMinor.clear();
1347 orientation.clear();
1348 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001349 tiltX.clear();
1350 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001351 trackingId.clear();
1352 slot.clear();
1353}
1354
1355
1356// --- RawPointerData ---
1357
1358RawPointerData::RawPointerData() {
1359 clear();
1360}
1361
1362void RawPointerData::clear() {
1363 pointerCount = 0;
1364 clearIdBits();
1365}
1366
1367void RawPointerData::copyFrom(const RawPointerData& other) {
1368 pointerCount = other.pointerCount;
1369 hoveringIdBits = other.hoveringIdBits;
1370 touchingIdBits = other.touchingIdBits;
1371
1372 for (uint32_t i = 0; i < pointerCount; i++) {
1373 pointers[i] = other.pointers[i];
1374
1375 int id = pointers[i].id;
1376 idToIndex[id] = other.idToIndex[id];
1377 }
1378}
1379
1380void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1381 float x = 0, y = 0;
1382 uint32_t count = touchingIdBits.count();
1383 if (count) {
1384 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1385 uint32_t id = idBits.clearFirstMarkedBit();
1386 const Pointer& pointer = pointerForId(id);
1387 x += pointer.x;
1388 y += pointer.y;
1389 }
1390 x /= count;
1391 y /= count;
1392 }
1393 *outX = x;
1394 *outY = y;
1395}
1396
1397
1398// --- CookedPointerData ---
1399
1400CookedPointerData::CookedPointerData() {
1401 clear();
1402}
1403
1404void CookedPointerData::clear() {
1405 pointerCount = 0;
1406 hoveringIdBits.clear();
1407 touchingIdBits.clear();
1408}
1409
1410void CookedPointerData::copyFrom(const CookedPointerData& other) {
1411 pointerCount = other.pointerCount;
1412 hoveringIdBits = other.hoveringIdBits;
1413 touchingIdBits = other.touchingIdBits;
1414
1415 for (uint32_t i = 0; i < pointerCount; i++) {
1416 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1417 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1418
1419 int id = pointerProperties[i].id;
1420 idToIndex[id] = other.idToIndex[id];
1421 }
1422}
1423
1424
Jeff Brown49754db2011-07-01 17:37:58 -07001425// --- SingleTouchMotionAccumulator ---
1426
1427SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1428 clearAbsoluteAxes();
1429}
1430
Jeff Brown65fd2512011-08-18 11:20:58 -07001431void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1432 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1433 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1434 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1435 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1436 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1437 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1438 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1439}
1440
Jeff Brown49754db2011-07-01 17:37:58 -07001441void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1442 mAbsX = 0;
1443 mAbsY = 0;
1444 mAbsPressure = 0;
1445 mAbsToolWidth = 0;
1446 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001447 mAbsTiltX = 0;
1448 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001449}
1450
1451void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1452 if (rawEvent->type == EV_ABS) {
1453 switch (rawEvent->scanCode) {
1454 case ABS_X:
1455 mAbsX = rawEvent->value;
1456 break;
1457 case ABS_Y:
1458 mAbsY = rawEvent->value;
1459 break;
1460 case ABS_PRESSURE:
1461 mAbsPressure = rawEvent->value;
1462 break;
1463 case ABS_TOOL_WIDTH:
1464 mAbsToolWidth = rawEvent->value;
1465 break;
1466 case ABS_DISTANCE:
1467 mAbsDistance = rawEvent->value;
1468 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001469 case ABS_TILT_X:
1470 mAbsTiltX = rawEvent->value;
1471 break;
1472 case ABS_TILT_Y:
1473 mAbsTiltY = rawEvent->value;
1474 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001475 }
1476 }
1477}
1478
1479
1480// --- MultiTouchMotionAccumulator ---
1481
1482MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1483 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1484}
1485
1486MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1487 delete[] mSlots;
1488}
1489
1490void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1491 mSlotCount = slotCount;
1492 mUsingSlotsProtocol = usingSlotsProtocol;
1493
1494 delete[] mSlots;
1495 mSlots = new Slot[slotCount];
1496}
1497
Jeff Brown65fd2512011-08-18 11:20:58 -07001498void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1499 // Unfortunately there is no way to read the initial contents of the slots.
1500 // So when we reset the accumulator, we must assume they are all zeroes.
1501 if (mUsingSlotsProtocol) {
1502 // Query the driver for the current slot index and use it as the initial slot
1503 // before we start reading events from the device. It is possible that the
1504 // current slot index will not be the same as it was when the first event was
1505 // written into the evdev buffer, which means the input mapper could start
1506 // out of sync with the initial state of the events in the evdev buffer.
1507 // In the extremely unlikely case that this happens, the data from
1508 // two slots will be confused until the next ABS_MT_SLOT event is received.
1509 // This can cause the touch point to "jump", but at least there will be
1510 // no stuck touches.
1511 int32_t initialSlot;
1512 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1513 ABS_MT_SLOT, &initialSlot);
1514 if (status) {
1515 LOGD("Could not retrieve current multitouch slot index. status=%d", status);
1516 initialSlot = -1;
1517 }
1518 clearSlots(initialSlot);
1519 } else {
1520 clearSlots(-1);
1521 }
1522}
1523
Jeff Brown49754db2011-07-01 17:37:58 -07001524void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001525 if (mSlots) {
1526 for (size_t i = 0; i < mSlotCount; i++) {
1527 mSlots[i].clear();
1528 }
Jeff Brown49754db2011-07-01 17:37:58 -07001529 }
1530 mCurrentSlot = initialSlot;
1531}
1532
1533void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1534 if (rawEvent->type == EV_ABS) {
1535 bool newSlot = false;
1536 if (mUsingSlotsProtocol) {
1537 if (rawEvent->scanCode == ABS_MT_SLOT) {
1538 mCurrentSlot = rawEvent->value;
1539 newSlot = true;
1540 }
1541 } else if (mCurrentSlot < 0) {
1542 mCurrentSlot = 0;
1543 }
1544
1545 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1546#if DEBUG_POINTERS
1547 if (newSlot) {
1548 LOGW("MultiTouch device emitted invalid slot index %d but it "
1549 "should be between 0 and %d; ignoring this slot.",
1550 mCurrentSlot, mSlotCount - 1);
1551 }
1552#endif
1553 } else {
1554 Slot* slot = &mSlots[mCurrentSlot];
1555
1556 switch (rawEvent->scanCode) {
1557 case ABS_MT_POSITION_X:
1558 slot->mInUse = true;
1559 slot->mAbsMTPositionX = rawEvent->value;
1560 break;
1561 case ABS_MT_POSITION_Y:
1562 slot->mInUse = true;
1563 slot->mAbsMTPositionY = rawEvent->value;
1564 break;
1565 case ABS_MT_TOUCH_MAJOR:
1566 slot->mInUse = true;
1567 slot->mAbsMTTouchMajor = rawEvent->value;
1568 break;
1569 case ABS_MT_TOUCH_MINOR:
1570 slot->mInUse = true;
1571 slot->mAbsMTTouchMinor = rawEvent->value;
1572 slot->mHaveAbsMTTouchMinor = true;
1573 break;
1574 case ABS_MT_WIDTH_MAJOR:
1575 slot->mInUse = true;
1576 slot->mAbsMTWidthMajor = rawEvent->value;
1577 break;
1578 case ABS_MT_WIDTH_MINOR:
1579 slot->mInUse = true;
1580 slot->mAbsMTWidthMinor = rawEvent->value;
1581 slot->mHaveAbsMTWidthMinor = true;
1582 break;
1583 case ABS_MT_ORIENTATION:
1584 slot->mInUse = true;
1585 slot->mAbsMTOrientation = rawEvent->value;
1586 break;
1587 case ABS_MT_TRACKING_ID:
1588 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001589 // The slot is no longer in use but it retains its previous contents,
1590 // which may be reused for subsequent touches.
1591 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001592 } else {
1593 slot->mInUse = true;
1594 slot->mAbsMTTrackingId = rawEvent->value;
1595 }
1596 break;
1597 case ABS_MT_PRESSURE:
1598 slot->mInUse = true;
1599 slot->mAbsMTPressure = rawEvent->value;
1600 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001601 case ABS_MT_DISTANCE:
1602 slot->mInUse = true;
1603 slot->mAbsMTDistance = rawEvent->value;
1604 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001605 case ABS_MT_TOOL_TYPE:
1606 slot->mInUse = true;
1607 slot->mAbsMTToolType = rawEvent->value;
1608 slot->mHaveAbsMTToolType = true;
1609 break;
1610 }
1611 }
1612 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1613 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1614 mCurrentSlot += 1;
1615 }
1616}
1617
Jeff Brown65fd2512011-08-18 11:20:58 -07001618void MultiTouchMotionAccumulator::finishSync() {
1619 if (!mUsingSlotsProtocol) {
1620 clearSlots(-1);
1621 }
1622}
1623
Jeff Brown49754db2011-07-01 17:37:58 -07001624
1625// --- MultiTouchMotionAccumulator::Slot ---
1626
1627MultiTouchMotionAccumulator::Slot::Slot() {
1628 clear();
1629}
1630
Jeff Brown49754db2011-07-01 17:37:58 -07001631void MultiTouchMotionAccumulator::Slot::clear() {
1632 mInUse = false;
1633 mHaveAbsMTTouchMinor = false;
1634 mHaveAbsMTWidthMinor = false;
1635 mHaveAbsMTToolType = false;
1636 mAbsMTPositionX = 0;
1637 mAbsMTPositionY = 0;
1638 mAbsMTTouchMajor = 0;
1639 mAbsMTTouchMinor = 0;
1640 mAbsMTWidthMajor = 0;
1641 mAbsMTWidthMinor = 0;
1642 mAbsMTOrientation = 0;
1643 mAbsMTTrackingId = -1;
1644 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001645 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001646 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001647}
1648
1649int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1650 if (mHaveAbsMTToolType) {
1651 switch (mAbsMTToolType) {
1652 case MT_TOOL_FINGER:
1653 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1654 case MT_TOOL_PEN:
1655 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1656 }
1657 }
1658 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1659}
1660
1661
Jeff Brown6d0fec22010-07-23 21:28:06 -07001662// --- InputMapper ---
1663
1664InputMapper::InputMapper(InputDevice* device) :
1665 mDevice(device), mContext(device->getContext()) {
1666}
1667
1668InputMapper::~InputMapper() {
1669}
1670
1671void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1672 info->addSource(getSources());
1673}
1674
Jeff Brownef3d7e82010-09-30 14:33:04 -07001675void InputMapper::dump(String8& dump) {
1676}
1677
Jeff Brown65fd2512011-08-18 11:20:58 -07001678void InputMapper::configure(nsecs_t when,
1679 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001680}
1681
Jeff Brown65fd2512011-08-18 11:20:58 -07001682void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001683}
1684
Jeff Brownaa3855d2011-03-17 01:34:19 -07001685void InputMapper::timeoutExpired(nsecs_t when) {
1686}
1687
Jeff Brown6d0fec22010-07-23 21:28:06 -07001688int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1689 return AKEY_STATE_UNKNOWN;
1690}
1691
1692int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1693 return AKEY_STATE_UNKNOWN;
1694}
1695
1696int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1697 return AKEY_STATE_UNKNOWN;
1698}
1699
1700bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1701 const int32_t* keyCodes, uint8_t* outFlags) {
1702 return false;
1703}
1704
1705int32_t InputMapper::getMetaState() {
1706 return 0;
1707}
1708
Jeff Brown05dc66a2011-03-02 14:41:58 -08001709void InputMapper::fadePointer() {
1710}
1711
Jeff Brownbe1aa822011-07-27 16:04:54 -07001712status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1713 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1714}
1715
Jeff Browncb1404e2011-01-15 18:14:15 -08001716void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1717 const RawAbsoluteAxisInfo& axis, const char* name) {
1718 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001719 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1720 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001721 } else {
1722 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1723 }
1724}
1725
Jeff Brown6d0fec22010-07-23 21:28:06 -07001726
1727// --- SwitchInputMapper ---
1728
1729SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1730 InputMapper(device) {
1731}
1732
1733SwitchInputMapper::~SwitchInputMapper() {
1734}
1735
1736uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001737 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001738}
1739
1740void SwitchInputMapper::process(const RawEvent* rawEvent) {
1741 switch (rawEvent->type) {
1742 case EV_SW:
1743 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1744 break;
1745 }
1746}
1747
1748void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001749 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1750 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001751}
1752
1753int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1754 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1755}
1756
1757
1758// --- KeyboardInputMapper ---
1759
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001760KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001761 uint32_t source, int32_t keyboardType) :
1762 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001763 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001764}
1765
1766KeyboardInputMapper::~KeyboardInputMapper() {
1767}
1768
Jeff Brown6d0fec22010-07-23 21:28:06 -07001769uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001770 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001771}
1772
1773void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1774 InputMapper::populateDeviceInfo(info);
1775
1776 info->setKeyboardType(mKeyboardType);
1777}
1778
Jeff Brownef3d7e82010-09-30 14:33:04 -07001779void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001780 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1781 dumpParameters(dump);
1782 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001783 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001784 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1785 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1786 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001787}
1788
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001789
Jeff Brown65fd2512011-08-18 11:20:58 -07001790void KeyboardInputMapper::configure(nsecs_t when,
1791 const InputReaderConfiguration* config, uint32_t changes) {
1792 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001793
Jeff Brown474dcb52011-06-14 20:22:50 -07001794 if (!changes) { // first time only
1795 // Configure basic parameters.
1796 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07001797 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001798
Jeff Brown65fd2512011-08-18 11:20:58 -07001799 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1800 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1801 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
1802 false /*external*/, NULL, NULL, &mOrientation)) {
1803 mOrientation = DISPLAY_ORIENTATION_0;
1804 }
1805 } else {
1806 mOrientation = DISPLAY_ORIENTATION_0;
1807 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001808 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001809}
1810
1811void KeyboardInputMapper::configureParameters() {
1812 mParameters.orientationAware = false;
1813 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1814 mParameters.orientationAware);
1815
Jeff Brownbc68a592011-07-25 12:58:12 -07001816 mParameters.associatedDisplayId = -1;
1817 if (mParameters.orientationAware) {
1818 mParameters.associatedDisplayId = 0;
1819 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001820}
1821
1822void KeyboardInputMapper::dumpParameters(String8& dump) {
1823 dump.append(INDENT3 "Parameters:\n");
1824 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1825 mParameters.associatedDisplayId);
1826 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1827 toString(mParameters.orientationAware));
1828}
1829
Jeff Brown65fd2512011-08-18 11:20:58 -07001830void KeyboardInputMapper::reset(nsecs_t when) {
1831 mMetaState = AMETA_NONE;
1832 mDownTime = 0;
1833 mKeyDowns.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001834
Jeff Brownbe1aa822011-07-27 16:04:54 -07001835 resetLedState();
1836
Jeff Brown65fd2512011-08-18 11:20:58 -07001837 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001838}
1839
1840void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1841 switch (rawEvent->type) {
1842 case EV_KEY: {
1843 int32_t scanCode = rawEvent->scanCode;
1844 if (isKeyboardOrGamepadKey(scanCode)) {
1845 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1846 rawEvent->flags);
1847 }
1848 break;
1849 }
1850 }
1851}
1852
1853bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1854 return scanCode < BTN_MOUSE
1855 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001856 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001857 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001858}
1859
Jeff Brown6328cdc2010-07-29 18:18:33 -07001860void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1861 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001862
Jeff Brownbe1aa822011-07-27 16:04:54 -07001863 if (down) {
1864 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001865 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001866 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001867 }
Jeff Brownfe508922011-01-18 15:10:10 -08001868
Jeff Brownbe1aa822011-07-27 16:04:54 -07001869 // Add key down.
1870 ssize_t keyDownIndex = findKeyDown(scanCode);
1871 if (keyDownIndex >= 0) {
1872 // key repeat, be sure to use same keycode as before in case of rotation
1873 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001874 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001875 // key down
1876 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1877 && mContext->shouldDropVirtualKey(when,
1878 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001879 return;
1880 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001881
1882 mKeyDowns.push();
1883 KeyDown& keyDown = mKeyDowns.editTop();
1884 keyDown.keyCode = keyCode;
1885 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001886 }
1887
Jeff Brownbe1aa822011-07-27 16:04:54 -07001888 mDownTime = when;
1889 } else {
1890 // Remove key down.
1891 ssize_t keyDownIndex = findKeyDown(scanCode);
1892 if (keyDownIndex >= 0) {
1893 // key up, be sure to use same keycode as before in case of rotation
1894 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1895 mKeyDowns.removeAt(size_t(keyDownIndex));
1896 } else {
1897 // key was not actually down
1898 LOGI("Dropping key up from device %s because the key was not down. "
1899 "keyCode=%d, scanCode=%d",
1900 getDeviceName().string(), keyCode, scanCode);
1901 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001902 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001903 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001904
Jeff Brownbe1aa822011-07-27 16:04:54 -07001905 bool metaStateChanged = false;
1906 int32_t oldMetaState = mMetaState;
1907 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1908 if (oldMetaState != newMetaState) {
1909 mMetaState = newMetaState;
1910 metaStateChanged = true;
1911 updateLedState(false);
1912 }
1913
1914 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001915
Jeff Brown56194eb2011-03-02 19:23:13 -08001916 // Key down on external an keyboard should wake the device.
1917 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1918 // For internal keyboards, the key layout file should specify the policy flags for
1919 // each wake key individually.
1920 // TODO: Use the input device configuration to control this behavior more finely.
1921 if (down && getDevice()->isExternal()
1922 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1923 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1924 }
1925
Jeff Brown6328cdc2010-07-29 18:18:33 -07001926 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001927 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001928 }
1929
Jeff Brown05dc66a2011-03-02 14:41:58 -08001930 if (down && !isMetaKey(keyCode)) {
1931 getContext()->fadePointer();
1932 }
1933
Jeff Brownbe1aa822011-07-27 16:04:54 -07001934 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001935 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1936 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001937 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001938}
1939
Jeff Brownbe1aa822011-07-27 16:04:54 -07001940ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1941 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001942 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001943 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001944 return i;
1945 }
1946 }
1947 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001948}
1949
Jeff Brown6d0fec22010-07-23 21:28:06 -07001950int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1951 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1952}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001953
Jeff Brown6d0fec22010-07-23 21:28:06 -07001954int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1955 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1956}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001957
Jeff Brown6d0fec22010-07-23 21:28:06 -07001958bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1959 const int32_t* keyCodes, uint8_t* outFlags) {
1960 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1961}
1962
1963int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001964 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001965}
1966
Jeff Brownbe1aa822011-07-27 16:04:54 -07001967void KeyboardInputMapper::resetLedState() {
1968 initializeLedState(mCapsLockLedState, LED_CAPSL);
1969 initializeLedState(mNumLockLedState, LED_NUML);
1970 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001971
Jeff Brownbe1aa822011-07-27 16:04:54 -07001972 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001973}
1974
Jeff Brownbe1aa822011-07-27 16:04:54 -07001975void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001976 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1977 ledState.on = false;
1978}
1979
Jeff Brownbe1aa822011-07-27 16:04:54 -07001980void KeyboardInputMapper::updateLedState(bool reset) {
1981 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001982 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001983 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001984 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001985 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001986 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001987}
1988
Jeff Brownbe1aa822011-07-27 16:04:54 -07001989void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07001990 int32_t led, int32_t modifier, bool reset) {
1991 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001992 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001993 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001994 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1995 ledState.on = desiredState;
1996 }
1997 }
1998}
1999
Jeff Brown6d0fec22010-07-23 21:28:06 -07002000
Jeff Brown83c09682010-12-23 17:50:18 -08002001// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002002
Jeff Brown83c09682010-12-23 17:50:18 -08002003CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002004 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002005}
2006
Jeff Brown83c09682010-12-23 17:50:18 -08002007CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002008}
2009
Jeff Brown83c09682010-12-23 17:50:18 -08002010uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002011 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002012}
2013
Jeff Brown83c09682010-12-23 17:50:18 -08002014void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002015 InputMapper::populateDeviceInfo(info);
2016
Jeff Brown83c09682010-12-23 17:50:18 -08002017 if (mParameters.mode == Parameters::MODE_POINTER) {
2018 float minX, minY, maxX, maxY;
2019 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002020 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2021 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002022 }
2023 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002024 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2025 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002026 }
Jeff Brownefd32662011-03-08 15:13:06 -08002027 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002028
Jeff Brown65fd2512011-08-18 11:20:58 -07002029 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002030 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002031 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002032 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002033 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002034 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002035}
2036
Jeff Brown83c09682010-12-23 17:50:18 -08002037void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002038 dump.append(INDENT2 "Cursor Input Mapper:\n");
2039 dumpParameters(dump);
2040 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2041 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2042 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2043 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2044 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002045 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002046 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002047 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002048 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2049 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002050 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002051 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2052 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2053 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002054}
2055
Jeff Brown65fd2512011-08-18 11:20:58 -07002056void CursorInputMapper::configure(nsecs_t when,
2057 const InputReaderConfiguration* config, uint32_t changes) {
2058 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002059
Jeff Brown474dcb52011-06-14 20:22:50 -07002060 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002061 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002062
Jeff Brown474dcb52011-06-14 20:22:50 -07002063 // Configure basic parameters.
2064 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002065
Jeff Brown474dcb52011-06-14 20:22:50 -07002066 // Configure device mode.
2067 switch (mParameters.mode) {
2068 case Parameters::MODE_POINTER:
2069 mSource = AINPUT_SOURCE_MOUSE;
2070 mXPrecision = 1.0f;
2071 mYPrecision = 1.0f;
2072 mXScale = 1.0f;
2073 mYScale = 1.0f;
2074 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2075 break;
2076 case Parameters::MODE_NAVIGATION:
2077 mSource = AINPUT_SOURCE_TRACKBALL;
2078 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2079 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2080 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2081 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2082 break;
2083 }
2084
2085 mVWheelScale = 1.0f;
2086 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002087 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002088
Jeff Brown474dcb52011-06-14 20:22:50 -07002089 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2090 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2091 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2092 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2093 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002094
2095 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2096 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2097 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2098 false /*external*/, NULL, NULL, &mOrientation)) {
2099 mOrientation = DISPLAY_ORIENTATION_0;
2100 }
2101 } else {
2102 mOrientation = DISPLAY_ORIENTATION_0;
2103 }
2104 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002105}
2106
Jeff Brown83c09682010-12-23 17:50:18 -08002107void CursorInputMapper::configureParameters() {
2108 mParameters.mode = Parameters::MODE_POINTER;
2109 String8 cursorModeString;
2110 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2111 if (cursorModeString == "navigation") {
2112 mParameters.mode = Parameters::MODE_NAVIGATION;
2113 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
2114 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
2115 }
2116 }
2117
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002118 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002119 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002120 mParameters.orientationAware);
2121
Jeff Brownbc68a592011-07-25 12:58:12 -07002122 mParameters.associatedDisplayId = -1;
2123 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2124 mParameters.associatedDisplayId = 0;
2125 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002126}
2127
Jeff Brown83c09682010-12-23 17:50:18 -08002128void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002129 dump.append(INDENT3 "Parameters:\n");
2130 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2131 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002132
2133 switch (mParameters.mode) {
2134 case Parameters::MODE_POINTER:
2135 dump.append(INDENT4 "Mode: pointer\n");
2136 break;
2137 case Parameters::MODE_NAVIGATION:
2138 dump.append(INDENT4 "Mode: navigation\n");
2139 break;
2140 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002141 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002142 }
2143
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002144 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2145 toString(mParameters.orientationAware));
2146}
2147
Jeff Brown65fd2512011-08-18 11:20:58 -07002148void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002149 mButtonState = 0;
2150 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002151
Jeff Brownbe1aa822011-07-27 16:04:54 -07002152 mPointerVelocityControl.reset();
2153 mWheelXVelocityControl.reset();
2154 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002155
Jeff Brown65fd2512011-08-18 11:20:58 -07002156 mCursorButtonAccumulator.reset(getDevice());
2157 mCursorMotionAccumulator.reset(getDevice());
2158 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002159
Jeff Brown65fd2512011-08-18 11:20:58 -07002160 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002161}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002162
Jeff Brown83c09682010-12-23 17:50:18 -08002163void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002164 mCursorButtonAccumulator.process(rawEvent);
2165 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002166 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002167
Jeff Brown49754db2011-07-01 17:37:58 -07002168 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
2169 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002170 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002171}
2172
Jeff Brown83c09682010-12-23 17:50:18 -08002173void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002174 int32_t lastButtonState = mButtonState;
2175 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2176 mButtonState = currentButtonState;
2177
2178 bool wasDown = isPointerDown(lastButtonState);
2179 bool down = isPointerDown(currentButtonState);
2180 bool downChanged;
2181 if (!wasDown && down) {
2182 mDownTime = when;
2183 downChanged = true;
2184 } else if (wasDown && !down) {
2185 downChanged = true;
2186 } else {
2187 downChanged = false;
2188 }
2189 nsecs_t downTime = mDownTime;
2190 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002191 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002192
2193 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2194 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2195 bool moved = deltaX != 0 || deltaY != 0;
2196
Jeff Brown65fd2512011-08-18 11:20:58 -07002197 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002198 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2199 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002200 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002201 }
2202
Jeff Brown65fd2512011-08-18 11:20:58 -07002203 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002204 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002205 pointerProperties.clear();
2206 pointerProperties.id = 0;
2207 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2208
Jeff Brown6328cdc2010-07-29 18:18:33 -07002209 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002210 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002211
Jeff Brown65fd2512011-08-18 11:20:58 -07002212 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2213 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002214 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002215
Jeff Brownbe1aa822011-07-27 16:04:54 -07002216 mWheelYVelocityControl.move(when, NULL, &vscroll);
2217 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002218
Jeff Brownbe1aa822011-07-27 16:04:54 -07002219 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002220
Jeff Brownbe1aa822011-07-27 16:04:54 -07002221 if (mPointerController != NULL) {
2222 if (moved || scrolled || buttonsChanged) {
2223 mPointerController->setPresentation(
2224 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002225
Jeff Brownbe1aa822011-07-27 16:04:54 -07002226 if (moved) {
2227 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002228 }
2229
Jeff Brownbe1aa822011-07-27 16:04:54 -07002230 if (buttonsChanged) {
2231 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002232 }
Jeff Brownefd32662011-03-08 15:13:06 -08002233
Jeff Brownbe1aa822011-07-27 16:04:54 -07002234 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002235 }
2236
Jeff Brownbe1aa822011-07-27 16:04:54 -07002237 float x, y;
2238 mPointerController->getPosition(&x, &y);
2239 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2240 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2241 } else {
2242 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2243 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2244 }
2245
2246 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002247
Jeff Brown56194eb2011-03-02 19:23:13 -08002248 // Moving an external trackball or mouse should wake the device.
2249 // We don't do this for internal cursor devices to prevent them from waking up
2250 // the device in your pocket.
2251 // TODO: Use the input device configuration to control this behavior more finely.
2252 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002253 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002254 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2255 }
2256
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002257 // Synthesize key down from buttons if needed.
2258 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2259 policyFlags, lastButtonState, currentButtonState);
2260
2261 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002262 if (downChanged || moved || scrolled || buttonsChanged) {
2263 int32_t metaState = mContext->getGlobalMetaState();
2264 int32_t motionEventAction;
2265 if (downChanged) {
2266 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2267 } else if (down || mPointerController == NULL) {
2268 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2269 } else {
2270 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2271 }
Jeff Brownb6997262010-10-08 22:31:17 -07002272
Jeff Brownbe1aa822011-07-27 16:04:54 -07002273 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2274 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002275 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002276 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002277
Jeff Brownbe1aa822011-07-27 16:04:54 -07002278 // Send hover move after UP to tell the application that the mouse is hovering now.
2279 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2280 && mPointerController != NULL) {
2281 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2282 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2283 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2284 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2285 getListener()->notifyMotion(&hoverArgs);
2286 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002287
Jeff Brownbe1aa822011-07-27 16:04:54 -07002288 // Send scroll events.
2289 if (scrolled) {
2290 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2291 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2292
2293 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2294 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2295 AMOTION_EVENT_EDGE_FLAG_NONE,
2296 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2297 getListener()->notifyMotion(&scrollArgs);
2298 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002299 }
Jeff Browna032cc02011-03-07 16:56:21 -08002300
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002301 // Synthesize key up from buttons if needed.
2302 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2303 policyFlags, lastButtonState, currentButtonState);
2304
Jeff Brown65fd2512011-08-18 11:20:58 -07002305 mCursorMotionAccumulator.finishSync();
2306 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002307}
2308
Jeff Brown83c09682010-12-23 17:50:18 -08002309int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002310 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2311 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2312 } else {
2313 return AKEY_STATE_UNKNOWN;
2314 }
2315}
2316
Jeff Brown05dc66a2011-03-02 14:41:58 -08002317void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002318 if (mPointerController != NULL) {
2319 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2320 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002321}
2322
Jeff Brown6d0fec22010-07-23 21:28:06 -07002323
2324// --- TouchInputMapper ---
2325
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002326TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002327 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002328 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002329 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002330}
2331
2332TouchInputMapper::~TouchInputMapper() {
2333}
2334
2335uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002336 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002337}
2338
2339void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2340 InputMapper::populateDeviceInfo(info);
2341
Jeff Brown65fd2512011-08-18 11:20:58 -07002342 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2343 info->addMotionRange(mOrientedRanges.x);
2344 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002345 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002346
Jeff Brown65fd2512011-08-18 11:20:58 -07002347 if (mOrientedRanges.haveSize) {
2348 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002349 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002350
2351 if (mOrientedRanges.haveTouchSize) {
2352 info->addMotionRange(mOrientedRanges.touchMajor);
2353 info->addMotionRange(mOrientedRanges.touchMinor);
2354 }
2355
2356 if (mOrientedRanges.haveToolSize) {
2357 info->addMotionRange(mOrientedRanges.toolMajor);
2358 info->addMotionRange(mOrientedRanges.toolMinor);
2359 }
2360
2361 if (mOrientedRanges.haveOrientation) {
2362 info->addMotionRange(mOrientedRanges.orientation);
2363 }
2364
2365 if (mOrientedRanges.haveDistance) {
2366 info->addMotionRange(mOrientedRanges.distance);
2367 }
2368
2369 if (mOrientedRanges.haveTilt) {
2370 info->addMotionRange(mOrientedRanges.tilt);
2371 }
2372
2373 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2374 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2375 }
2376 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2377 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2378 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002379 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002380}
2381
Jeff Brownef3d7e82010-09-30 14:33:04 -07002382void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002383 dump.append(INDENT2 "Touch Input Mapper:\n");
2384 dumpParameters(dump);
2385 dumpVirtualKeys(dump);
2386 dumpRawPointerAxes(dump);
2387 dumpCalibration(dump);
2388 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002389
Jeff Brownbe1aa822011-07-27 16:04:54 -07002390 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2391 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2392 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2393 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2394 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2395 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002396 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2397 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002398 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002399 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2400 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002401 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2402 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2403 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2404 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2405 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002406
Jeff Brownbe1aa822011-07-27 16:04:54 -07002407 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002408
Jeff Brownbe1aa822011-07-27 16:04:54 -07002409 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2410 mLastRawPointerData.pointerCount);
2411 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2412 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2413 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2414 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002415 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2416 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002417 pointer.id, pointer.x, pointer.y, pointer.pressure,
2418 pointer.touchMajor, pointer.touchMinor,
2419 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002420 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002421 pointer.toolType, toString(pointer.isHovering));
2422 }
2423
2424 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2425 mLastCookedPointerData.pointerCount);
2426 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2427 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2428 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2429 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2430 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002431 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2432 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002433 pointerProperties.id,
2434 pointerCoords.getX(),
2435 pointerCoords.getY(),
2436 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2437 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2438 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2439 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2440 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2441 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002442 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002443 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2444 pointerProperties.toolType,
2445 toString(mLastCookedPointerData.isHovering(i)));
2446 }
2447
Jeff Brown65fd2512011-08-18 11:20:58 -07002448 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002449 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2450 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002451 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002452 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002453 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002454 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002455 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002456 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002457 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002458 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2459 mPointerGestureMaxSwipeWidth);
2460 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002461}
2462
Jeff Brown65fd2512011-08-18 11:20:58 -07002463void TouchInputMapper::configure(nsecs_t when,
2464 const InputReaderConfiguration* config, uint32_t changes) {
2465 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002466
Jeff Brown474dcb52011-06-14 20:22:50 -07002467 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002468
Jeff Brown474dcb52011-06-14 20:22:50 -07002469 if (!changes) { // first time only
2470 // Configure basic parameters.
2471 configureParameters();
2472
Jeff Brown65fd2512011-08-18 11:20:58 -07002473 // Configure common accumulators.
2474 mCursorScrollAccumulator.configure(getDevice());
2475 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002476
2477 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002478 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002479
2480 // Prepare input device calibration.
2481 parseCalibration();
2482 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002483 }
2484
Jeff Brown474dcb52011-06-14 20:22:50 -07002485 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002486 // Update pointer speed.
2487 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2488 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2489 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002490 }
Jeff Brown8d608662010-08-30 03:02:23 -07002491
Jeff Brown65fd2512011-08-18 11:20:58 -07002492 bool resetNeeded = false;
2493 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
2494 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT))) {
2495 // Configure device sources, surface dimensions, orientation and
2496 // scaling factors.
2497 configureSurface(when, &resetNeeded);
2498 }
2499
2500 if (changes && resetNeeded) {
2501 // Send reset, unless this is the first time the device has been configured,
2502 // in which case the reader will call reset itself after all mappers are ready.
2503 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002504 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002505}
2506
Jeff Brown8d608662010-08-30 03:02:23 -07002507void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002508 // Use the pointer presentation mode for devices that do not support distinct
2509 // multitouch. The spot-based presentation relies on being able to accurately
2510 // locate two or more fingers on the touch pad.
2511 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2512 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002513
Jeff Brown538881e2011-05-25 18:23:38 -07002514 String8 gestureModeString;
2515 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2516 gestureModeString)) {
2517 if (gestureModeString == "pointer") {
2518 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2519 } else if (gestureModeString == "spots") {
2520 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2521 } else if (gestureModeString != "default") {
2522 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2523 }
2524 }
2525
Jeff Brownace13b12011-03-09 17:39:48 -08002526 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2527 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2528 // The device is a cursor device with a touch pad attached.
2529 // By default don't use the touch pad to move the pointer.
2530 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002531 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2532 // The device is a pointing device like a track pad.
2533 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2534 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2535 // The device is a touch screen.
2536 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002537 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002538 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002539 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2540 }
2541
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002542 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002543 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2544 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002545 if (deviceTypeString == "touchScreen") {
2546 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002547 } else if (deviceTypeString == "touchPad") {
2548 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002549 } else if (deviceTypeString == "pointer") {
2550 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002551 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002552 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2553 }
2554 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002555
Jeff Brownefd32662011-03-08 15:13:06 -08002556 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002557 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2558 mParameters.orientationAware);
2559
Jeff Brownbc68a592011-07-25 12:58:12 -07002560 mParameters.associatedDisplayId = -1;
2561 mParameters.associatedDisplayIsExternal = false;
2562 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002563 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002564 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2565 mParameters.associatedDisplayIsExternal =
2566 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2567 && getDevice()->isExternal();
2568 mParameters.associatedDisplayId = 0;
2569 }
Jeff Brown8d608662010-08-30 03:02:23 -07002570}
2571
Jeff Brownef3d7e82010-09-30 14:33:04 -07002572void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002573 dump.append(INDENT3 "Parameters:\n");
2574
Jeff Brown538881e2011-05-25 18:23:38 -07002575 switch (mParameters.gestureMode) {
2576 case Parameters::GESTURE_MODE_POINTER:
2577 dump.append(INDENT4 "GestureMode: pointer\n");
2578 break;
2579 case Parameters::GESTURE_MODE_SPOTS:
2580 dump.append(INDENT4 "GestureMode: spots\n");
2581 break;
2582 default:
2583 assert(false);
2584 }
2585
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002586 switch (mParameters.deviceType) {
2587 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2588 dump.append(INDENT4 "DeviceType: touchScreen\n");
2589 break;
2590 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2591 dump.append(INDENT4 "DeviceType: touchPad\n");
2592 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002593 case Parameters::DEVICE_TYPE_POINTER:
2594 dump.append(INDENT4 "DeviceType: pointer\n");
2595 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002596 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002597 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002598 }
2599
Jeff Brown65fd2512011-08-18 11:20:58 -07002600 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2601 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002602 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2603 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002604}
2605
Jeff Brownbe1aa822011-07-27 16:04:54 -07002606void TouchInputMapper::configureRawPointerAxes() {
2607 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002608}
2609
Jeff Brownbe1aa822011-07-27 16:04:54 -07002610void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2611 dump.append(INDENT3 "Raw Touch Axes:\n");
2612 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2613 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2614 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2615 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2616 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2617 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2618 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2619 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2620 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002621 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2622 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002623 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2624 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002625}
2626
Jeff Brown65fd2512011-08-18 11:20:58 -07002627void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2628 int32_t oldDeviceMode = mDeviceMode;
2629
2630 // Determine device mode.
2631 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2632 && mConfig.pointerGesturesEnabled) {
2633 mSource = AINPUT_SOURCE_MOUSE;
2634 mDeviceMode = DEVICE_MODE_POINTER;
2635 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2636 && mParameters.associatedDisplayId >= 0) {
2637 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2638 mDeviceMode = DEVICE_MODE_DIRECT;
2639 } else {
2640 mSource = AINPUT_SOURCE_TOUCHPAD;
2641 mDeviceMode = DEVICE_MODE_UNSCALED;
2642 }
2643
Jeff Brown9626b142011-03-03 02:09:54 -08002644 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002645 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002646 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2647 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002648 mDeviceMode = DEVICE_MODE_DISABLED;
2649 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002650 }
2651
Jeff Brown65fd2512011-08-18 11:20:58 -07002652 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002653 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002654 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002655 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002656 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2657 &mAssociatedDisplayOrientation)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002658 LOGI(INDENT "Touch device '%s' could not query the properties of its associated "
2659 "display %d. The device will be inoperable until the display size "
2660 "becomes available.",
2661 getDeviceName().string(), mParameters.associatedDisplayId);
2662 mDeviceMode = DEVICE_MODE_DISABLED;
2663 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002664 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002665 }
2666
Jeff Brown65fd2512011-08-18 11:20:58 -07002667 // Configure dimensions.
2668 int32_t width, height, orientation;
2669 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2670 width = mAssociatedDisplayWidth;
2671 height = mAssociatedDisplayHeight;
2672 orientation = mParameters.orientationAware ?
2673 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2674 } else {
2675 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2676 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2677 orientation = DISPLAY_ORIENTATION_0;
2678 }
2679
2680 // If moving between pointer modes, need to reset some state.
2681 bool deviceModeChanged;
2682 if (mDeviceMode != oldDeviceMode) {
2683 deviceModeChanged = true;
2684
2685 if (mDeviceMode == DEVICE_MODE_POINTER) {
2686 if (mPointerController == NULL) {
2687 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2688 }
2689 } else {
2690 mPointerController.clear();
2691 }
2692
2693 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002694 }
2695
Jeff Brownbe1aa822011-07-27 16:04:54 -07002696 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002697 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002698 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002699 }
2700
Jeff Brownbe1aa822011-07-27 16:04:54 -07002701 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002702 if (sizeChanged || deviceModeChanged) {
2703 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
2704 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002705
Jeff Brownbe1aa822011-07-27 16:04:54 -07002706 mSurfaceWidth = width;
2707 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002708
Jeff Brown8d608662010-08-30 03:02:23 -07002709 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002710 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2711 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2712 mXPrecision = 1.0f / mXScale;
2713 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002714
Jeff Brownbe1aa822011-07-27 16:04:54 -07002715 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002716 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002717 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002718 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002719
Jeff Brownbe1aa822011-07-27 16:04:54 -07002720 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002721
Jeff Brown8d608662010-08-30 03:02:23 -07002722 // Scale factor for terms that are not oriented in a particular axis.
2723 // If the pixels are square then xScale == yScale otherwise we fake it
2724 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002725 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002726
Jeff Brown8d608662010-08-30 03:02:23 -07002727 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002728 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002729
Jeff Browna1f89ce2011-08-11 00:05:01 -07002730 // Size factors.
2731 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2732 if (mRawPointerAxes.touchMajor.valid
2733 && mRawPointerAxes.touchMajor.maxValue != 0) {
2734 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2735 } else if (mRawPointerAxes.toolMajor.valid
2736 && mRawPointerAxes.toolMajor.maxValue != 0) {
2737 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2738 } else {
2739 mSizeScale = 0.0f;
2740 }
2741
Jeff Brownbe1aa822011-07-27 16:04:54 -07002742 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002743 mOrientedRanges.haveToolSize = true;
2744 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002745
Jeff Brownbe1aa822011-07-27 16:04:54 -07002746 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002747 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002748 mOrientedRanges.touchMajor.min = 0;
2749 mOrientedRanges.touchMajor.max = diagonalSize;
2750 mOrientedRanges.touchMajor.flat = 0;
2751 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002752
Jeff Brownbe1aa822011-07-27 16:04:54 -07002753 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2754 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002755
Jeff Brownbe1aa822011-07-27 16:04:54 -07002756 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002757 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002758 mOrientedRanges.toolMajor.min = 0;
2759 mOrientedRanges.toolMajor.max = diagonalSize;
2760 mOrientedRanges.toolMajor.flat = 0;
2761 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002762
Jeff Brownbe1aa822011-07-27 16:04:54 -07002763 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2764 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002765
2766 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002767 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002768 mOrientedRanges.size.min = 0;
2769 mOrientedRanges.size.max = 1.0;
2770 mOrientedRanges.size.flat = 0;
2771 mOrientedRanges.size.fuzz = 0;
2772 } else {
2773 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002774 }
2775
2776 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002777 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002778 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2779 || mCalibration.pressureCalibration
2780 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2781 if (mCalibration.havePressureScale) {
2782 mPressureScale = mCalibration.pressureScale;
2783 } else if (mRawPointerAxes.pressure.valid
2784 && mRawPointerAxes.pressure.maxValue != 0) {
2785 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002786 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002787 }
Jeff Brown8d608662010-08-30 03:02:23 -07002788
Jeff Brown65fd2512011-08-18 11:20:58 -07002789 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2790 mOrientedRanges.pressure.source = mSource;
2791 mOrientedRanges.pressure.min = 0;
2792 mOrientedRanges.pressure.max = 1.0;
2793 mOrientedRanges.pressure.flat = 0;
2794 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002795
Jeff Brown65fd2512011-08-18 11:20:58 -07002796 // Tilt
2797 mTiltXCenter = 0;
2798 mTiltXScale = 0;
2799 mTiltYCenter = 0;
2800 mTiltYScale = 0;
2801 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2802 if (mHaveTilt) {
2803 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2804 mRawPointerAxes.tiltX.maxValue);
2805 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2806 mRawPointerAxes.tiltY.maxValue);
2807 mTiltXScale = M_PI / 180;
2808 mTiltYScale = M_PI / 180;
2809
2810 mOrientedRanges.haveTilt = true;
2811
2812 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2813 mOrientedRanges.tilt.source = mSource;
2814 mOrientedRanges.tilt.min = 0;
2815 mOrientedRanges.tilt.max = M_PI_2;
2816 mOrientedRanges.tilt.flat = 0;
2817 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002818 }
2819
Jeff Brown8d608662010-08-30 03:02:23 -07002820 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002821 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002822 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002823 if (mHaveTilt) {
2824 mOrientedRanges.haveOrientation = true;
2825
2826 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2827 mOrientedRanges.orientation.source = mSource;
2828 mOrientedRanges.orientation.min = -M_PI;
2829 mOrientedRanges.orientation.max = M_PI;
2830 mOrientedRanges.orientation.flat = 0;
2831 mOrientedRanges.orientation.fuzz = 0;
2832 } else if (mCalibration.orientationCalibration !=
2833 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002834 if (mCalibration.orientationCalibration
2835 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002836 if (mRawPointerAxes.orientation.valid) {
2837 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2838 mRawPointerAxes.orientation.maxValue);
2839 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2840 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002841 }
2842 }
2843
Jeff Brownbe1aa822011-07-27 16:04:54 -07002844 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002845
Jeff Brownbe1aa822011-07-27 16:04:54 -07002846 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002847 mOrientedRanges.orientation.source = mSource;
2848 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002849 mOrientedRanges.orientation.max = M_PI_2;
2850 mOrientedRanges.orientation.flat = 0;
2851 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002852 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002853
2854 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002855 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002856 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2857 if (mCalibration.distanceCalibration
2858 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2859 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002860 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002861 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002862 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002863 }
2864 }
2865
Jeff Brownbe1aa822011-07-27 16:04:54 -07002866 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002867
Jeff Brownbe1aa822011-07-27 16:04:54 -07002868 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002869 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002870 mOrientedRanges.distance.min =
2871 mRawPointerAxes.distance.minValue * mDistanceScale;
2872 mOrientedRanges.distance.max =
2873 mRawPointerAxes.distance.minValue * mDistanceScale;
2874 mOrientedRanges.distance.flat = 0;
2875 mOrientedRanges.distance.fuzz =
2876 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002877 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002878 }
2879
Jeff Brown65fd2512011-08-18 11:20:58 -07002880 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002881 // Compute oriented surface dimensions, precision, scales and ranges.
2882 // Note that the maximum value reported is an inclusive maximum value so it is one
2883 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002884 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002885 case DISPLAY_ORIENTATION_90:
2886 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002887 mOrientedSurfaceWidth = mSurfaceHeight;
2888 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002889
Jeff Brownbe1aa822011-07-27 16:04:54 -07002890 mOrientedXPrecision = mYPrecision;
2891 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002892
Jeff Brownbe1aa822011-07-27 16:04:54 -07002893 mOrientedRanges.x.min = 0;
2894 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2895 * mYScale;
2896 mOrientedRanges.x.flat = 0;
2897 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002898
Jeff Brownbe1aa822011-07-27 16:04:54 -07002899 mOrientedRanges.y.min = 0;
2900 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2901 * mXScale;
2902 mOrientedRanges.y.flat = 0;
2903 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002904 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002905
Jeff Brown6d0fec22010-07-23 21:28:06 -07002906 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002907 mOrientedSurfaceWidth = mSurfaceWidth;
2908 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002909
Jeff Brownbe1aa822011-07-27 16:04:54 -07002910 mOrientedXPrecision = mXPrecision;
2911 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002912
Jeff Brownbe1aa822011-07-27 16:04:54 -07002913 mOrientedRanges.x.min = 0;
2914 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2915 * mXScale;
2916 mOrientedRanges.x.flat = 0;
2917 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002918
Jeff Brownbe1aa822011-07-27 16:04:54 -07002919 mOrientedRanges.y.min = 0;
2920 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2921 * mYScale;
2922 mOrientedRanges.y.flat = 0;
2923 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002924 break;
2925 }
Jeff Brownace13b12011-03-09 17:39:48 -08002926
2927 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002928 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002929 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2930 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002931 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002932 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2933 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002934
Jeff Brown2352b972011-04-12 22:39:53 -07002935 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002936 // given area relative to the diagonal size of the display when no acceleration
2937 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002938 // Assume that the touch pad has a square aspect ratio such that movements in
2939 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07002940 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002941 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002942 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002943
2944 // Scale zooms to cover a smaller range of the display than movements do.
2945 // This value determines the area around the pointer that is affected by freeform
2946 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07002947 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002948 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002949 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002950
Jeff Brown2352b972011-04-12 22:39:53 -07002951 // Max width between pointers to detect a swipe gesture is more than some fraction
2952 // of the diagonal axis of the touch pad. Touches that are wider than this are
2953 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002954 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002955 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002956 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002957
Jeff Brown65fd2512011-08-18 11:20:58 -07002958 // Abort current pointer usages because the state has changed.
2959 abortPointerUsage(when, 0 /*policyFlags*/);
2960
2961 // Inform the dispatcher about the changes.
2962 *outResetNeeded = true;
2963 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002964}
2965
Jeff Brownbe1aa822011-07-27 16:04:54 -07002966void TouchInputMapper::dumpSurface(String8& dump) {
2967 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2968 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2969 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002970}
2971
Jeff Brownbe1aa822011-07-27 16:04:54 -07002972void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002973 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002974 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002975
Jeff Brownbe1aa822011-07-27 16:04:54 -07002976 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002977
Jeff Brown6328cdc2010-07-29 18:18:33 -07002978 if (virtualKeyDefinitions.size() == 0) {
2979 return;
2980 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002981
Jeff Brownbe1aa822011-07-27 16:04:54 -07002982 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002983
Jeff Brownbe1aa822011-07-27 16:04:54 -07002984 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2985 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2986 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2987 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002988
2989 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002990 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002991 virtualKeyDefinitions[i];
2992
Jeff Brownbe1aa822011-07-27 16:04:54 -07002993 mVirtualKeys.add();
2994 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002995
2996 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2997 int32_t keyCode;
2998 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002999 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07003000 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003001 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
3002 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003003 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003004 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003005 }
3006
Jeff Brown6328cdc2010-07-29 18:18:33 -07003007 virtualKey.keyCode = keyCode;
3008 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003009
Jeff Brown6328cdc2010-07-29 18:18:33 -07003010 // convert the key definition's display coordinates into touch coordinates for a hit box
3011 int32_t halfWidth = virtualKeyDefinition.width / 2;
3012 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003013
Jeff Brown6328cdc2010-07-29 18:18:33 -07003014 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003015 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003016 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003017 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003018 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003019 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003020 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003021 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003022 }
3023}
3024
Jeff Brownbe1aa822011-07-27 16:04:54 -07003025void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3026 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003027 dump.append(INDENT3 "Virtual Keys:\n");
3028
Jeff Brownbe1aa822011-07-27 16:04:54 -07003029 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3030 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003031 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3032 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3033 i, virtualKey.scanCode, virtualKey.keyCode,
3034 virtualKey.hitLeft, virtualKey.hitRight,
3035 virtualKey.hitTop, virtualKey.hitBottom);
3036 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003037 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003038}
3039
Jeff Brown8d608662010-08-30 03:02:23 -07003040void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003041 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003042 Calibration& out = mCalibration;
3043
Jeff Browna1f89ce2011-08-11 00:05:01 -07003044 // Size
3045 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3046 String8 sizeCalibrationString;
3047 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3048 if (sizeCalibrationString == "none") {
3049 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3050 } else if (sizeCalibrationString == "geometric") {
3051 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3052 } else if (sizeCalibrationString == "diameter") {
3053 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3054 } else if (sizeCalibrationString == "area") {
3055 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3056 } else if (sizeCalibrationString != "default") {
3057 LOGW("Invalid value for touch.size.calibration: '%s'",
3058 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003059 }
3060 }
3061
Jeff Browna1f89ce2011-08-11 00:05:01 -07003062 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3063 out.sizeScale);
3064 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3065 out.sizeBias);
3066 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3067 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003068
3069 // Pressure
3070 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3071 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003072 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003073 if (pressureCalibrationString == "none") {
3074 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3075 } else if (pressureCalibrationString == "physical") {
3076 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3077 } else if (pressureCalibrationString == "amplitude") {
3078 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3079 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003080 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003081 pressureCalibrationString.string());
3082 }
3083 }
3084
Jeff Brown8d608662010-08-30 03:02:23 -07003085 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3086 out.pressureScale);
3087
Jeff Brown8d608662010-08-30 03:02:23 -07003088 // Orientation
3089 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3090 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003091 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003092 if (orientationCalibrationString == "none") {
3093 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3094 } else if (orientationCalibrationString == "interpolated") {
3095 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003096 } else if (orientationCalibrationString == "vector") {
3097 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003098 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003099 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003100 orientationCalibrationString.string());
3101 }
3102 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003103
3104 // Distance
3105 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3106 String8 distanceCalibrationString;
3107 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3108 if (distanceCalibrationString == "none") {
3109 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3110 } else if (distanceCalibrationString == "scaled") {
3111 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3112 } else if (distanceCalibrationString != "default") {
3113 LOGW("Invalid value for touch.distance.calibration: '%s'",
3114 distanceCalibrationString.string());
3115 }
3116 }
3117
3118 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3119 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003120}
3121
3122void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003123 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003124 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3125 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3126 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003127 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003128 } else {
3129 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3130 }
Jeff Brown8d608662010-08-30 03:02:23 -07003131
Jeff Browna1f89ce2011-08-11 00:05:01 -07003132 // Pressure
3133 if (mRawPointerAxes.pressure.valid) {
3134 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3135 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3136 }
3137 } else {
3138 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003139 }
3140
3141 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003142 if (mRawPointerAxes.orientation.valid) {
3143 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003144 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003145 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003146 } else {
3147 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003148 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003149
3150 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003151 if (mRawPointerAxes.distance.valid) {
3152 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003153 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003154 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003155 } else {
3156 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003157 }
Jeff Brown8d608662010-08-30 03:02:23 -07003158}
3159
Jeff Brownef3d7e82010-09-30 14:33:04 -07003160void TouchInputMapper::dumpCalibration(String8& dump) {
3161 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003162
Jeff Browna1f89ce2011-08-11 00:05:01 -07003163 // Size
3164 switch (mCalibration.sizeCalibration) {
3165 case Calibration::SIZE_CALIBRATION_NONE:
3166 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003167 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003168 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3169 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003170 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003171 case Calibration::SIZE_CALIBRATION_DIAMETER:
3172 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3173 break;
3174 case Calibration::SIZE_CALIBRATION_AREA:
3175 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003176 break;
3177 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003178 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003179 }
3180
Jeff Browna1f89ce2011-08-11 00:05:01 -07003181 if (mCalibration.haveSizeScale) {
3182 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3183 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003184 }
3185
Jeff Browna1f89ce2011-08-11 00:05:01 -07003186 if (mCalibration.haveSizeBias) {
3187 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3188 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003189 }
3190
Jeff Browna1f89ce2011-08-11 00:05:01 -07003191 if (mCalibration.haveSizeIsSummed) {
3192 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3193 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003194 }
3195
3196 // Pressure
3197 switch (mCalibration.pressureCalibration) {
3198 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003199 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003200 break;
3201 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003202 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003203 break;
3204 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003205 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003206 break;
3207 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003208 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003209 }
3210
Jeff Brown8d608662010-08-30 03:02:23 -07003211 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003212 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3213 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003214 }
3215
Jeff Brown8d608662010-08-30 03:02:23 -07003216 // Orientation
3217 switch (mCalibration.orientationCalibration) {
3218 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003219 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003220 break;
3221 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003222 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003223 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003224 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3225 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3226 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003227 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003228 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003229 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003230
3231 // Distance
3232 switch (mCalibration.distanceCalibration) {
3233 case Calibration::DISTANCE_CALIBRATION_NONE:
3234 dump.append(INDENT4 "touch.distance.calibration: none\n");
3235 break;
3236 case Calibration::DISTANCE_CALIBRATION_SCALED:
3237 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3238 break;
3239 default:
3240 LOG_ASSERT(false);
3241 }
3242
3243 if (mCalibration.haveDistanceScale) {
3244 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3245 mCalibration.distanceScale);
3246 }
Jeff Brown8d608662010-08-30 03:02:23 -07003247}
3248
Jeff Brown65fd2512011-08-18 11:20:58 -07003249void TouchInputMapper::reset(nsecs_t when) {
3250 mCursorButtonAccumulator.reset(getDevice());
3251 mCursorScrollAccumulator.reset(getDevice());
3252 mTouchButtonAccumulator.reset(getDevice());
3253
3254 mPointerVelocityControl.reset();
3255 mWheelXVelocityControl.reset();
3256 mWheelYVelocityControl.reset();
3257
Jeff Brownbe1aa822011-07-27 16:04:54 -07003258 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003259 mLastRawPointerData.clear();
3260 mCurrentCookedPointerData.clear();
3261 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003262 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003263 mLastButtonState = 0;
3264 mCurrentRawVScroll = 0;
3265 mCurrentRawHScroll = 0;
3266 mCurrentFingerIdBits.clear();
3267 mLastFingerIdBits.clear();
3268 mCurrentStylusIdBits.clear();
3269 mLastStylusIdBits.clear();
3270 mCurrentMouseIdBits.clear();
3271 mLastMouseIdBits.clear();
3272 mPointerUsage = POINTER_USAGE_NONE;
3273 mSentHoverEnter = false;
3274 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003275
Jeff Brown65fd2512011-08-18 11:20:58 -07003276 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003277
Jeff Brown65fd2512011-08-18 11:20:58 -07003278 mPointerGesture.reset();
3279 mPointerSimple.reset();
3280
3281 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003282 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3283 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003284 }
3285
Jeff Brown65fd2512011-08-18 11:20:58 -07003286 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003287}
3288
Jeff Brown65fd2512011-08-18 11:20:58 -07003289void TouchInputMapper::process(const RawEvent* rawEvent) {
3290 mCursorButtonAccumulator.process(rawEvent);
3291 mCursorScrollAccumulator.process(rawEvent);
3292 mTouchButtonAccumulator.process(rawEvent);
3293
3294 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
3295 sync(rawEvent->when);
3296 }
3297}
3298
3299void TouchInputMapper::sync(nsecs_t when) {
3300 // Sync button state.
3301 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3302 | mCursorButtonAccumulator.getButtonState();
3303
3304 // Sync scroll state.
3305 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3306 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3307 mCursorScrollAccumulator.finishSync();
3308
3309 // Sync touch state.
3310 bool havePointerIds = true;
3311 mCurrentRawPointerData.clear();
3312 syncTouch(when, &havePointerIds);
3313
Jeff Brownaa3855d2011-03-17 01:34:19 -07003314#if DEBUG_RAW_EVENTS
3315 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003316 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3317 mLastRawPointerData.pointerCount,
3318 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003319 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003320 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3321 "hovering ids 0x%08x -> 0x%08x",
3322 mLastRawPointerData.pointerCount,
3323 mCurrentRawPointerData.pointerCount,
3324 mLastRawPointerData.touchingIdBits.value,
3325 mCurrentRawPointerData.touchingIdBits.value,
3326 mLastRawPointerData.hoveringIdBits.value,
3327 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003328 }
3329#endif
3330
Jeff Brown65fd2512011-08-18 11:20:58 -07003331 // Reset state that we will compute below.
3332 mCurrentFingerIdBits.clear();
3333 mCurrentStylusIdBits.clear();
3334 mCurrentMouseIdBits.clear();
3335 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003336
Jeff Brown65fd2512011-08-18 11:20:58 -07003337 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3338 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003339 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003340 mCurrentButtonState = 0;
3341 } else {
3342 // Preprocess pointer data.
3343 if (!havePointerIds) {
3344 assignPointerIds();
3345 }
3346
3347 // Handle policy on initial down or hover events.
3348 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003349 bool initialDown = mLastRawPointerData.pointerCount == 0
3350 && mCurrentRawPointerData.pointerCount != 0;
3351 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3352 if (initialDown || buttonsPressed) {
3353 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003354 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003355 getContext()->fadePointer();
3356 }
3357
3358 // Initial downs on external touch devices should wake the device.
3359 // We don't do this for internal touch screens to prevent them from waking
3360 // up in your pocket.
3361 // TODO: Use the input device configuration to control this behavior more finely.
3362 if (getDevice()->isExternal()) {
3363 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3364 }
3365 }
3366
3367 // Synthesize key down from raw buttons if needed.
3368 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3369 policyFlags, mLastButtonState, mCurrentButtonState);
3370
3371 // Consume raw off-screen touches before cooking pointer data.
3372 // If touches are consumed, subsequent code will not receive any pointer data.
3373 if (consumeRawTouches(when, policyFlags)) {
3374 mCurrentRawPointerData.clear();
3375 }
3376
3377 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3378 // with cooked pointer data that has the same ids and indices as the raw data.
3379 // The following code can use either the raw or cooked data, as needed.
3380 cookPointerData();
3381
3382 // Dispatch the touches either directly or by translation through a pointer on screen.
3383 if (mPointerController != NULL) {
3384 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3385 uint32_t id = idBits.clearFirstMarkedBit();
3386 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3387 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3388 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3389 mCurrentStylusIdBits.markBit(id);
3390 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3391 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3392 mCurrentFingerIdBits.markBit(id);
3393 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3394 mCurrentMouseIdBits.markBit(id);
3395 }
3396 }
3397 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3398 uint32_t id = idBits.clearFirstMarkedBit();
3399 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3400 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3401 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3402 mCurrentStylusIdBits.markBit(id);
3403 }
3404 }
3405
3406 // Stylus takes precedence over all tools, then mouse, then finger.
3407 PointerUsage pointerUsage = mPointerUsage;
3408 if (!mCurrentStylusIdBits.isEmpty()) {
3409 mCurrentMouseIdBits.clear();
3410 mCurrentFingerIdBits.clear();
3411 pointerUsage = POINTER_USAGE_STYLUS;
3412 } else if (!mCurrentMouseIdBits.isEmpty()) {
3413 mCurrentFingerIdBits.clear();
3414 pointerUsage = POINTER_USAGE_MOUSE;
3415 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3416 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003417 }
3418
3419 dispatchPointerUsage(when, policyFlags, pointerUsage);
3420 } else {
3421 dispatchHoverExit(when, policyFlags);
3422 dispatchTouches(when, policyFlags);
3423 dispatchHoverEnterAndMove(when, policyFlags);
3424 }
3425
3426 // Synthesize key up from raw buttons if needed.
3427 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3428 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003429 }
3430
Jeff Brown6328cdc2010-07-29 18:18:33 -07003431 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003432 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3433 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3434 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003435 mLastFingerIdBits = mCurrentFingerIdBits;
3436 mLastStylusIdBits = mCurrentStylusIdBits;
3437 mLastMouseIdBits = mCurrentMouseIdBits;
3438
3439 // Clear some transient state.
3440 mCurrentRawVScroll = 0;
3441 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003442}
3443
Jeff Brown79ac9692011-04-19 21:20:10 -07003444void TouchInputMapper::timeoutExpired(nsecs_t when) {
3445 if (mPointerController != NULL) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003446 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3447 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3448 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003449 }
3450}
3451
Jeff Brownbe1aa822011-07-27 16:04:54 -07003452bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3453 // Check for release of a virtual key.
3454 if (mCurrentVirtualKey.down) {
3455 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3456 // Pointer went up while virtual key was down.
3457 mCurrentVirtualKey.down = false;
3458 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003459#if DEBUG_VIRTUAL_KEYS
3460 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003461 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003462#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003463 dispatchVirtualKey(when, policyFlags,
3464 AKEY_EVENT_ACTION_UP,
3465 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003466 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003467 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003468 }
3469
Jeff Brownbe1aa822011-07-27 16:04:54 -07003470 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3471 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3472 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3473 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3474 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3475 // Pointer is still within the space of the virtual key.
3476 return true;
3477 }
3478 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003479
Jeff Brownbe1aa822011-07-27 16:04:54 -07003480 // Pointer left virtual key area or another pointer also went down.
3481 // Send key cancellation but do not consume the touch yet.
3482 // This is useful when the user swipes through from the virtual key area
3483 // into the main display surface.
3484 mCurrentVirtualKey.down = false;
3485 if (!mCurrentVirtualKey.ignored) {
3486#if DEBUG_VIRTUAL_KEYS
3487 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3488 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3489#endif
3490 dispatchVirtualKey(when, policyFlags,
3491 AKEY_EVENT_ACTION_UP,
3492 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3493 | AKEY_EVENT_FLAG_CANCELED);
3494 }
3495 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003496
Jeff Brownbe1aa822011-07-27 16:04:54 -07003497 if (mLastRawPointerData.touchingIdBits.isEmpty()
3498 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3499 // Pointer just went down. Check for virtual key press or off-screen touches.
3500 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3501 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3502 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3503 // If exactly one pointer went down, check for virtual key hit.
3504 // Otherwise we will drop the entire stroke.
3505 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3506 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3507 if (virtualKey) {
3508 mCurrentVirtualKey.down = true;
3509 mCurrentVirtualKey.downTime = when;
3510 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3511 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3512 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3513 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3514
3515 if (!mCurrentVirtualKey.ignored) {
3516#if DEBUG_VIRTUAL_KEYS
3517 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3518 mCurrentVirtualKey.keyCode,
3519 mCurrentVirtualKey.scanCode);
3520#endif
3521 dispatchVirtualKey(when, policyFlags,
3522 AKEY_EVENT_ACTION_DOWN,
3523 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3524 }
3525 }
3526 }
3527 return true;
3528 }
3529 }
3530
Jeff Brownfe508922011-01-18 15:10:10 -08003531 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003532 // most recent touch within the screen area. The idea is to filter out stray
3533 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003534 //
3535 // Problems we're trying to solve:
3536 //
3537 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3538 // virtual key area that is implemented by a separate touch panel and accidentally
3539 // triggers a virtual key.
3540 //
3541 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3542 // area and accidentally triggers a virtual key. This often happens when virtual keys
3543 // are layed out below the screen near to where the on screen keyboard's space bar
3544 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003545 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003546 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003547 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003548 return false;
3549}
3550
3551void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3552 int32_t keyEventAction, int32_t keyEventFlags) {
3553 int32_t keyCode = mCurrentVirtualKey.keyCode;
3554 int32_t scanCode = mCurrentVirtualKey.scanCode;
3555 nsecs_t downTime = mCurrentVirtualKey.downTime;
3556 int32_t metaState = mContext->getGlobalMetaState();
3557 policyFlags |= POLICY_FLAG_VIRTUAL;
3558
3559 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3560 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3561 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003562}
3563
Jeff Brown6d0fec22010-07-23 21:28:06 -07003564void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003565 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3566 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003567 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003568 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003569
3570 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003571 if (!currentIdBits.isEmpty()) {
3572 // No pointer id changes so this is a move event.
3573 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003574 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003575 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3576 AMOTION_EVENT_EDGE_FLAG_NONE,
3577 mCurrentCookedPointerData.pointerProperties,
3578 mCurrentCookedPointerData.pointerCoords,
3579 mCurrentCookedPointerData.idToIndex,
3580 currentIdBits, -1,
3581 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3582 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003583 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003584 // There may be pointers going up and pointers going down and pointers moving
3585 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003586 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3587 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003588 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003589 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003590
Jeff Brownace13b12011-03-09 17:39:48 -08003591 // Update last coordinates of pointers that have moved so that we observe the new
3592 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003593 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003594 mCurrentCookedPointerData.pointerProperties,
3595 mCurrentCookedPointerData.pointerCoords,
3596 mCurrentCookedPointerData.idToIndex,
3597 mLastCookedPointerData.pointerProperties,
3598 mLastCookedPointerData.pointerCoords,
3599 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003600 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003601 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003602 moveNeeded = true;
3603 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003604
Jeff Brownace13b12011-03-09 17:39:48 -08003605 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003606 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003607 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003608
Jeff Brown65fd2512011-08-18 11:20:58 -07003609 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003610 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003611 mLastCookedPointerData.pointerProperties,
3612 mLastCookedPointerData.pointerCoords,
3613 mLastCookedPointerData.idToIndex,
3614 dispatchedIdBits, upId,
3615 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003616 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003617 }
3618
Jeff Brownc3db8582010-10-20 15:33:38 -07003619 // Dispatch move events if any of the remaining pointers moved from their old locations.
3620 // Although applications receive new locations as part of individual pointer up
3621 // events, they do not generally handle them except when presented in a move event.
3622 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003623 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003624 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003625 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003626 mCurrentCookedPointerData.pointerProperties,
3627 mCurrentCookedPointerData.pointerCoords,
3628 mCurrentCookedPointerData.idToIndex,
3629 dispatchedIdBits, -1,
3630 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003631 }
3632
3633 // Dispatch pointer down events using the new pointer locations.
3634 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003635 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003636 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003637
Jeff Brownace13b12011-03-09 17:39:48 -08003638 if (dispatchedIdBits.count() == 1) {
3639 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003640 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003641 }
3642
Jeff Brown65fd2512011-08-18 11:20:58 -07003643 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003644 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003645 mCurrentCookedPointerData.pointerProperties,
3646 mCurrentCookedPointerData.pointerCoords,
3647 mCurrentCookedPointerData.idToIndex,
3648 dispatchedIdBits, downId,
3649 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003650 }
3651 }
Jeff Brownace13b12011-03-09 17:39:48 -08003652}
3653
Jeff Brownbe1aa822011-07-27 16:04:54 -07003654void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3655 if (mSentHoverEnter &&
3656 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3657 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3658 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003659 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003660 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3661 mLastCookedPointerData.pointerProperties,
3662 mLastCookedPointerData.pointerCoords,
3663 mLastCookedPointerData.idToIndex,
3664 mLastCookedPointerData.hoveringIdBits, -1,
3665 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3666 mSentHoverEnter = false;
3667 }
3668}
Jeff Brownace13b12011-03-09 17:39:48 -08003669
Jeff Brownbe1aa822011-07-27 16:04:54 -07003670void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3671 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3672 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3673 int32_t metaState = getContext()->getGlobalMetaState();
3674 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003675 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003676 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3677 mCurrentCookedPointerData.pointerProperties,
3678 mCurrentCookedPointerData.pointerCoords,
3679 mCurrentCookedPointerData.idToIndex,
3680 mCurrentCookedPointerData.hoveringIdBits, -1,
3681 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3682 mSentHoverEnter = true;
3683 }
Jeff Brownace13b12011-03-09 17:39:48 -08003684
Jeff Brown65fd2512011-08-18 11:20:58 -07003685 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003686 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3687 mCurrentCookedPointerData.pointerProperties,
3688 mCurrentCookedPointerData.pointerCoords,
3689 mCurrentCookedPointerData.idToIndex,
3690 mCurrentCookedPointerData.hoveringIdBits, -1,
3691 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3692 }
3693}
3694
3695void TouchInputMapper::cookPointerData() {
3696 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3697
3698 mCurrentCookedPointerData.clear();
3699 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3700 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3701 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3702
3703 // Walk through the the active pointers and map device coordinates onto
3704 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003705 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003706 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003707
Jeff Browna1f89ce2011-08-11 00:05:01 -07003708 // Size
3709 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3710 switch (mCalibration.sizeCalibration) {
3711 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3712 case Calibration::SIZE_CALIBRATION_DIAMETER:
3713 case Calibration::SIZE_CALIBRATION_AREA:
3714 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3715 touchMajor = in.touchMajor;
3716 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3717 toolMajor = in.toolMajor;
3718 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3719 size = mRawPointerAxes.touchMinor.valid
3720 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3721 } else if (mRawPointerAxes.touchMajor.valid) {
3722 toolMajor = touchMajor = in.touchMajor;
3723 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3724 ? in.touchMinor : in.touchMajor;
3725 size = mRawPointerAxes.touchMinor.valid
3726 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3727 } else if (mRawPointerAxes.toolMajor.valid) {
3728 touchMajor = toolMajor = in.toolMajor;
3729 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3730 ? in.toolMinor : in.toolMajor;
3731 size = mRawPointerAxes.toolMinor.valid
3732 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003733 } else {
Jeff Browna1f89ce2011-08-11 00:05:01 -07003734 LOG_ASSERT(false, "No touch or tool axes. "
3735 "Size calibration should have been resolved to NONE.");
3736 touchMajor = 0;
3737 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003738 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003739 toolMinor = 0;
3740 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003741 }
Jeff Brownace13b12011-03-09 17:39:48 -08003742
Jeff Browna1f89ce2011-08-11 00:05:01 -07003743 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3744 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3745 if (touchingCount > 1) {
3746 touchMajor /= touchingCount;
3747 touchMinor /= touchingCount;
3748 toolMajor /= touchingCount;
3749 toolMinor /= touchingCount;
3750 size /= touchingCount;
3751 }
3752 }
Jeff Brownace13b12011-03-09 17:39:48 -08003753
Jeff Browna1f89ce2011-08-11 00:05:01 -07003754 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3755 touchMajor *= mGeometricScale;
3756 touchMinor *= mGeometricScale;
3757 toolMajor *= mGeometricScale;
3758 toolMinor *= mGeometricScale;
3759 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3760 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003761 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003762 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3763 toolMinor = toolMajor;
3764 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3765 touchMinor = touchMajor;
3766 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003767 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003768
3769 mCalibration.applySizeScaleAndBias(&touchMajor);
3770 mCalibration.applySizeScaleAndBias(&touchMinor);
3771 mCalibration.applySizeScaleAndBias(&toolMajor);
3772 mCalibration.applySizeScaleAndBias(&toolMinor);
3773 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003774 break;
3775 default:
3776 touchMajor = 0;
3777 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003778 toolMajor = 0;
3779 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003780 size = 0;
3781 break;
3782 }
3783
Jeff Browna1f89ce2011-08-11 00:05:01 -07003784 // Pressure
3785 float pressure;
3786 switch (mCalibration.pressureCalibration) {
3787 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3788 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3789 pressure = in.pressure * mPressureScale;
3790 break;
3791 default:
3792 pressure = in.isHovering ? 0 : 1;
3793 break;
3794 }
3795
Jeff Brown65fd2512011-08-18 11:20:58 -07003796 // Tilt and Orientation
3797 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003798 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003799 if (mHaveTilt) {
3800 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3801 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3802 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3803 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3804 } else {
3805 tilt = 0;
3806
3807 switch (mCalibration.orientationCalibration) {
3808 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3809 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3810 break;
3811 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3812 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3813 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3814 if (c1 != 0 || c2 != 0) {
3815 orientation = atan2f(c1, c2) * 0.5f;
3816 float confidence = hypotf(c1, c2);
3817 float scale = 1.0f + confidence / 16.0f;
3818 touchMajor *= scale;
3819 touchMinor /= scale;
3820 toolMajor *= scale;
3821 toolMinor /= scale;
3822 } else {
3823 orientation = 0;
3824 }
3825 break;
3826 }
3827 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003828 orientation = 0;
3829 }
Jeff Brownace13b12011-03-09 17:39:48 -08003830 }
3831
Jeff Brown80fd47c2011-05-24 01:07:44 -07003832 // Distance
3833 float distance;
3834 switch (mCalibration.distanceCalibration) {
3835 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003836 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003837 break;
3838 default:
3839 distance = 0;
3840 }
3841
Jeff Brownace13b12011-03-09 17:39:48 -08003842 // X and Y
3843 // Adjust coords for surface orientation.
3844 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003845 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003846 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003847 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3848 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003849 orientation -= M_PI_2;
3850 if (orientation < - M_PI_2) {
3851 orientation += M_PI;
3852 }
3853 break;
3854 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003855 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3856 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003857 break;
3858 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003859 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3860 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003861 orientation += M_PI_2;
3862 if (orientation > M_PI_2) {
3863 orientation -= M_PI;
3864 }
3865 break;
3866 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003867 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3868 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003869 break;
3870 }
3871
3872 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003873 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003874 out.clear();
3875 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3876 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3877 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3878 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3879 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3880 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3881 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3882 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3883 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003884 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003885 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003886
3887 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003888 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3889 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003890 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003891 properties.id = id;
3892 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003893
Jeff Brownbe1aa822011-07-27 16:04:54 -07003894 // Write id index.
3895 mCurrentCookedPointerData.idToIndex[id] = i;
3896 }
Jeff Brownace13b12011-03-09 17:39:48 -08003897}
3898
Jeff Brown65fd2512011-08-18 11:20:58 -07003899void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3900 PointerUsage pointerUsage) {
3901 if (pointerUsage != mPointerUsage) {
3902 abortPointerUsage(when, policyFlags);
3903 mPointerUsage = pointerUsage;
3904 }
3905
3906 switch (mPointerUsage) {
3907 case POINTER_USAGE_GESTURES:
3908 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3909 break;
3910 case POINTER_USAGE_STYLUS:
3911 dispatchPointerStylus(when, policyFlags);
3912 break;
3913 case POINTER_USAGE_MOUSE:
3914 dispatchPointerMouse(when, policyFlags);
3915 break;
3916 default:
3917 break;
3918 }
3919}
3920
3921void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3922 switch (mPointerUsage) {
3923 case POINTER_USAGE_GESTURES:
3924 abortPointerGestures(when, policyFlags);
3925 break;
3926 case POINTER_USAGE_STYLUS:
3927 abortPointerStylus(when, policyFlags);
3928 break;
3929 case POINTER_USAGE_MOUSE:
3930 abortPointerMouse(when, policyFlags);
3931 break;
3932 default:
3933 break;
3934 }
3935
3936 mPointerUsage = POINTER_USAGE_NONE;
3937}
3938
Jeff Brown79ac9692011-04-19 21:20:10 -07003939void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3940 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003941 // Update current gesture coordinates.
3942 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003943 bool sendEvents = preparePointerGestures(when,
3944 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3945 if (!sendEvents) {
3946 return;
3947 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003948 if (finishPreviousGesture) {
3949 cancelPreviousGesture = false;
3950 }
Jeff Brownace13b12011-03-09 17:39:48 -08003951
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003952 // Update the pointer presentation and spots.
3953 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3954 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3955 if (finishPreviousGesture || cancelPreviousGesture) {
3956 mPointerController->clearSpots();
3957 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003958 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3959 mPointerGesture.currentGestureIdToIndex,
3960 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003961 } else {
3962 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3963 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003964
Jeff Brown538881e2011-05-25 18:23:38 -07003965 // Show or hide the pointer if needed.
3966 switch (mPointerGesture.currentGestureMode) {
3967 case PointerGesture::NEUTRAL:
3968 case PointerGesture::QUIET:
3969 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3970 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3971 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3972 // Remind the user of where the pointer is after finishing a gesture with spots.
3973 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3974 }
3975 break;
3976 case PointerGesture::TAP:
3977 case PointerGesture::TAP_DRAG:
3978 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3979 case PointerGesture::HOVER:
3980 case PointerGesture::PRESS:
3981 // Unfade the pointer when the current gesture manipulates the
3982 // area directly under the pointer.
3983 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3984 break;
3985 case PointerGesture::SWIPE:
3986 case PointerGesture::FREEFORM:
3987 // Fade the pointer when the current gesture manipulates a different
3988 // area and there are spots to guide the user experience.
3989 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3990 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3991 } else {
3992 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3993 }
3994 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003995 }
3996
Jeff Brownace13b12011-03-09 17:39:48 -08003997 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003998 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003999 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004000
4001 // Update last coordinates of pointers that have moved so that we observe the new
4002 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004003 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4004 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4005 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004006 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004007 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4008 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4009 bool moveNeeded = false;
4010 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004011 && !mPointerGesture.lastGestureIdBits.isEmpty()
4012 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004013 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4014 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004015 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004016 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004017 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004018 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4019 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004020 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004021 moveNeeded = true;
4022 }
Jeff Brownace13b12011-03-09 17:39:48 -08004023 }
4024
4025 // Send motion events for all pointers that went up or were canceled.
4026 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4027 if (!dispatchedGestureIdBits.isEmpty()) {
4028 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004029 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004030 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4031 AMOTION_EVENT_EDGE_FLAG_NONE,
4032 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004033 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4034 dispatchedGestureIdBits, -1,
4035 0, 0, mPointerGesture.downTime);
4036
4037 dispatchedGestureIdBits.clear();
4038 } else {
4039 BitSet32 upGestureIdBits;
4040 if (finishPreviousGesture) {
4041 upGestureIdBits = dispatchedGestureIdBits;
4042 } else {
4043 upGestureIdBits.value = dispatchedGestureIdBits.value
4044 & ~mPointerGesture.currentGestureIdBits.value;
4045 }
4046 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004047 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004048
Jeff Brown65fd2512011-08-18 11:20:58 -07004049 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004050 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004051 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4052 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004053 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4054 dispatchedGestureIdBits, id,
4055 0, 0, mPointerGesture.downTime);
4056
4057 dispatchedGestureIdBits.clearBit(id);
4058 }
4059 }
4060 }
4061
4062 // Send motion events for all pointers that moved.
4063 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004064 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004065 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4066 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004067 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4068 dispatchedGestureIdBits, -1,
4069 0, 0, mPointerGesture.downTime);
4070 }
4071
4072 // Send motion events for all pointers that went down.
4073 if (down) {
4074 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4075 & ~dispatchedGestureIdBits.value);
4076 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004077 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004078 dispatchedGestureIdBits.markBit(id);
4079
Jeff Brownace13b12011-03-09 17:39:48 -08004080 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004081 mPointerGesture.downTime = when;
4082 }
4083
Jeff Brown65fd2512011-08-18 11:20:58 -07004084 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004085 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004086 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004087 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4088 dispatchedGestureIdBits, id,
4089 0, 0, mPointerGesture.downTime);
4090 }
4091 }
4092
Jeff Brownace13b12011-03-09 17:39:48 -08004093 // Send motion events for hover.
4094 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004095 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004096 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4097 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4098 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004099 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4100 mPointerGesture.currentGestureIdBits, -1,
4101 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004102 } else if (dispatchedGestureIdBits.isEmpty()
4103 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4104 // Synthesize a hover move event after all pointers go up to indicate that
4105 // the pointer is hovering again even if the user is not currently touching
4106 // the touch pad. This ensures that a view will receive a fresh hover enter
4107 // event after a tap.
4108 float x, y;
4109 mPointerController->getPosition(&x, &y);
4110
4111 PointerProperties pointerProperties;
4112 pointerProperties.clear();
4113 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004114 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004115
4116 PointerCoords pointerCoords;
4117 pointerCoords.clear();
4118 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4119 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4120
Jeff Brown65fd2512011-08-18 11:20:58 -07004121 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004122 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4123 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4124 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004125 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004126 }
4127
4128 // Update state.
4129 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4130 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004131 mPointerGesture.lastGestureIdBits.clear();
4132 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004133 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4134 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004135 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004136 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004137 mPointerGesture.lastGestureProperties[index].copyFrom(
4138 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004139 mPointerGesture.lastGestureCoords[index].copyFrom(
4140 mPointerGesture.currentGestureCoords[index]);
4141 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004142 }
4143 }
4144}
4145
Jeff Brown65fd2512011-08-18 11:20:58 -07004146void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4147 // Cancel previously dispatches pointers.
4148 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4149 int32_t metaState = getContext()->getGlobalMetaState();
4150 int32_t buttonState = mCurrentButtonState;
4151 dispatchMotion(when, policyFlags, mSource,
4152 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4153 AMOTION_EVENT_EDGE_FLAG_NONE,
4154 mPointerGesture.lastGestureProperties,
4155 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4156 mPointerGesture.lastGestureIdBits, -1,
4157 0, 0, mPointerGesture.downTime);
4158 }
4159
4160 // Reset the current pointer gesture.
4161 mPointerGesture.reset();
4162 mPointerVelocityControl.reset();
4163
4164 // Remove any current spots.
4165 if (mPointerController != NULL) {
4166 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4167 mPointerController->clearSpots();
4168 }
4169}
4170
Jeff Brown79ac9692011-04-19 21:20:10 -07004171bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4172 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004173 *outCancelPreviousGesture = false;
4174 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004175
Jeff Brown79ac9692011-04-19 21:20:10 -07004176 // Handle TAP timeout.
4177 if (isTimeout) {
4178#if DEBUG_GESTURES
4179 LOGD("Gestures: Processing timeout");
4180#endif
4181
4182 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004183 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004184 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004185 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004186 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004187 } else {
4188 // The tap is finished.
4189#if DEBUG_GESTURES
4190 LOGD("Gestures: TAP finished");
4191#endif
4192 *outFinishPreviousGesture = true;
4193
4194 mPointerGesture.activeGestureId = -1;
4195 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4196 mPointerGesture.currentGestureIdBits.clear();
4197
Jeff Brown65fd2512011-08-18 11:20:58 -07004198 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004199 return true;
4200 }
4201 }
4202
4203 // We did not handle this timeout.
4204 return false;
4205 }
4206
Jeff Brown65fd2512011-08-18 11:20:58 -07004207 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4208 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4209
Jeff Brownace13b12011-03-09 17:39:48 -08004210 // Update the velocity tracker.
4211 {
4212 VelocityTracker::Position positions[MAX_POINTERS];
4213 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004214 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004215 uint32_t id = idBits.clearFirstMarkedBit();
4216 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004217 positions[count].x = pointer.x * mPointerXMovementScale;
4218 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004219 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004220 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004221 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004222 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004223
Jeff Brownace13b12011-03-09 17:39:48 -08004224 // Pick a new active touch id if needed.
4225 // Choose an arbitrary pointer that just went down, if there is one.
4226 // Otherwise choose an arbitrary remaining pointer.
4227 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004228 // We keep the same active touch id for as long as possible.
4229 bool activeTouchChanged = false;
4230 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4231 int32_t activeTouchId = lastActiveTouchId;
4232 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004233 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004234 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004235 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004236 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004237 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004238 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004239 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004240 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004241 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004242 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004243 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004244 } else {
4245 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004246 }
4247 }
4248
4249 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004250 bool isQuietTime = false;
4251 if (activeTouchId < 0) {
4252 mPointerGesture.resetQuietTime();
4253 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004254 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004255 if (!isQuietTime) {
4256 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4257 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4258 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004259 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004260 // Enter quiet time when exiting swipe or freeform state.
4261 // This is to prevent accidentally entering the hover state and flinging the
4262 // pointer when finishing a swipe and there is still one pointer left onscreen.
4263 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004264 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004265 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004266 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004267 // Enter quiet time when releasing the button and there are still two or more
4268 // fingers down. This may indicate that one finger was used to press the button
4269 // but it has not gone up yet.
4270 isQuietTime = true;
4271 }
4272 if (isQuietTime) {
4273 mPointerGesture.quietTime = when;
4274 }
Jeff Brownace13b12011-03-09 17:39:48 -08004275 }
4276 }
4277
4278 // Switch states based on button and pointer state.
4279 if (isQuietTime) {
4280 // Case 1: Quiet time. (QUIET)
4281#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004282 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004283 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004284#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004285 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4286 *outFinishPreviousGesture = true;
4287 }
Jeff Brownace13b12011-03-09 17:39:48 -08004288
4289 mPointerGesture.activeGestureId = -1;
4290 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004291 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004292
Jeff Brown65fd2512011-08-18 11:20:58 -07004293 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004294 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004295 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004296 // The pointer follows the active touch point.
4297 // Emit DOWN, MOVE, UP events at the pointer location.
4298 //
4299 // Only the active touch matters; other fingers are ignored. This policy helps
4300 // to handle the case where the user places a second finger on the touch pad
4301 // to apply the necessary force to depress an integrated button below the surface.
4302 // We don't want the second finger to be delivered to applications.
4303 //
4304 // For this to work well, we need to make sure to track the pointer that is really
4305 // active. If the user first puts one finger down to click then adds another
4306 // finger to drag then the active pointer should switch to the finger that is
4307 // being dragged.
4308#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004309 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004310 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004311#endif
4312 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004313 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004314 *outFinishPreviousGesture = true;
4315 mPointerGesture.activeGestureId = 0;
4316 }
4317
4318 // Switch pointers if needed.
4319 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004320 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004321 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004322 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004323 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004324 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004325 float vx, vy;
4326 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4327 float speed = hypotf(vx, vy);
4328 if (speed > bestSpeed) {
4329 bestId = id;
4330 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004331 }
Jeff Brown8d608662010-08-30 03:02:23 -07004332 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004333 }
4334 if (bestId >= 0 && bestId != activeTouchId) {
4335 mPointerGesture.activeTouchId = activeTouchId = bestId;
4336 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004337#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07004338 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4339 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004340#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004341 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004342 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004343
Jeff Brown65fd2512011-08-18 11:20:58 -07004344 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004345 const RawPointerData::Pointer& currentPointer =
4346 mCurrentRawPointerData.pointerForId(activeTouchId);
4347 const RawPointerData::Pointer& lastPointer =
4348 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004349 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4350 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004351
Jeff Brownbe1aa822011-07-27 16:04:54 -07004352 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004353 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004354
4355 // Move the pointer using a relative motion.
4356 // When using spots, the click will occur at the position of the anchor
4357 // spot and all other spots will move there.
4358 mPointerController->move(deltaX, deltaY);
4359 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004360 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004361 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004362
Jeff Brownace13b12011-03-09 17:39:48 -08004363 float x, y;
4364 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004365
Jeff Brown79ac9692011-04-19 21:20:10 -07004366 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004367 mPointerGesture.currentGestureIdBits.clear();
4368 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4369 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004370 mPointerGesture.currentGestureProperties[0].clear();
4371 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004372 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004373 mPointerGesture.currentGestureCoords[0].clear();
4374 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4375 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4376 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004377 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004378 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004379 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4380 *outFinishPreviousGesture = true;
4381 }
Jeff Brownace13b12011-03-09 17:39:48 -08004382
Jeff Brown79ac9692011-04-19 21:20:10 -07004383 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004384 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004385 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004386 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4387 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004388 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004389 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004390 float x, y;
4391 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004392 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4393 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004394#if DEBUG_GESTURES
4395 LOGD("Gestures: TAP");
4396#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004397
4398 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004399 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004400 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004401
Jeff Brownace13b12011-03-09 17:39:48 -08004402 mPointerGesture.activeGestureId = 0;
4403 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004404 mPointerGesture.currentGestureIdBits.clear();
4405 mPointerGesture.currentGestureIdBits.markBit(
4406 mPointerGesture.activeGestureId);
4407 mPointerGesture.currentGestureIdToIndex[
4408 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004409 mPointerGesture.currentGestureProperties[0].clear();
4410 mPointerGesture.currentGestureProperties[0].id =
4411 mPointerGesture.activeGestureId;
4412 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004413 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004414 mPointerGesture.currentGestureCoords[0].clear();
4415 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004416 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004417 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004418 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004419 mPointerGesture.currentGestureCoords[0].setAxisValue(
4420 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004421
Jeff Brownace13b12011-03-09 17:39:48 -08004422 tapped = true;
4423 } else {
4424#if DEBUG_GESTURES
4425 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004426 x - mPointerGesture.tapX,
4427 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004428#endif
4429 }
4430 } else {
4431#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004432 LOGD("Gestures: Not a TAP, %0.3fms since down",
4433 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004434#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004435 }
Jeff Brownace13b12011-03-09 17:39:48 -08004436 }
Jeff Brown2352b972011-04-12 22:39:53 -07004437
Jeff Brown65fd2512011-08-18 11:20:58 -07004438 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004439
Jeff Brownace13b12011-03-09 17:39:48 -08004440 if (!tapped) {
4441#if DEBUG_GESTURES
4442 LOGD("Gestures: NEUTRAL");
4443#endif
4444 mPointerGesture.activeGestureId = -1;
4445 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004446 mPointerGesture.currentGestureIdBits.clear();
4447 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004448 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004449 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004450 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004451 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4452 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004453 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004454
Jeff Brown79ac9692011-04-19 21:20:10 -07004455 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4456 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004457 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004458 float x, y;
4459 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004460 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4461 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004462 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4463 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004464#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004465 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4466 x - mPointerGesture.tapX,
4467 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004468#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004469 }
4470 } else {
4471#if DEBUG_GESTURES
4472 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4473 (when - mPointerGesture.tapUpTime) * 0.000001f);
4474#endif
4475 }
4476 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4477 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4478 }
Jeff Brownace13b12011-03-09 17:39:48 -08004479
Jeff Brown65fd2512011-08-18 11:20:58 -07004480 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004481 const RawPointerData::Pointer& currentPointer =
4482 mCurrentRawPointerData.pointerForId(activeTouchId);
4483 const RawPointerData::Pointer& lastPointer =
4484 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004485 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004486 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004487 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004488 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004489
Jeff Brownbe1aa822011-07-27 16:04:54 -07004490 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004491 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004492
Jeff Brown2352b972011-04-12 22:39:53 -07004493 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004494 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004495 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004496 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004497 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004498 }
4499
Jeff Brown79ac9692011-04-19 21:20:10 -07004500 bool down;
4501 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4502#if DEBUG_GESTURES
4503 LOGD("Gestures: TAP_DRAG");
4504#endif
4505 down = true;
4506 } else {
4507#if DEBUG_GESTURES
4508 LOGD("Gestures: HOVER");
4509#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004510 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4511 *outFinishPreviousGesture = true;
4512 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004513 mPointerGesture.activeGestureId = 0;
4514 down = false;
4515 }
Jeff Brownace13b12011-03-09 17:39:48 -08004516
4517 float x, y;
4518 mPointerController->getPosition(&x, &y);
4519
Jeff Brownace13b12011-03-09 17:39:48 -08004520 mPointerGesture.currentGestureIdBits.clear();
4521 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4522 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004523 mPointerGesture.currentGestureProperties[0].clear();
4524 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4525 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004526 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004527 mPointerGesture.currentGestureCoords[0].clear();
4528 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4529 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004530 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4531 down ? 1.0f : 0.0f);
4532
Jeff Brown65fd2512011-08-18 11:20:58 -07004533 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004534 mPointerGesture.resetTap();
4535 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004536 mPointerGesture.tapX = x;
4537 mPointerGesture.tapY = y;
4538 }
Jeff Brownace13b12011-03-09 17:39:48 -08004539 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004540 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4541 // We need to provide feedback for each finger that goes down so we cannot wait
4542 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004543 //
Jeff Brown2352b972011-04-12 22:39:53 -07004544 // The ambiguous case is deciding what to do when there are two fingers down but they
4545 // have not moved enough to determine whether they are part of a drag or part of a
4546 // freeform gesture, or just a press or long-press at the pointer location.
4547 //
4548 // When there are two fingers we start with the PRESS hypothesis and we generate a
4549 // down at the pointer location.
4550 //
4551 // When the two fingers move enough or when additional fingers are added, we make
4552 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004553 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004554
Jeff Brown214eaf42011-05-26 19:17:02 -07004555 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004556 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004557 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004558 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4559 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004560 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004561 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004562 // Additional pointers have gone down but not yet settled.
4563 // Reset the gesture.
4564#if DEBUG_GESTURES
4565 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004566 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004567 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004568 * 0.000001f);
4569#endif
4570 *outCancelPreviousGesture = true;
4571 } else {
4572 // Continue previous gesture.
4573 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4574 }
4575
4576 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004577 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4578 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004579 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004580 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004581
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004582 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004583#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004584 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4585 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004586 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004587 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004588#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004589 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4590 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004591 &mPointerGesture.referenceTouchY);
4592 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4593 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004594 }
Jeff Brownace13b12011-03-09 17:39:48 -08004595
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004596 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004597 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004598 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4599 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004600 mPointerGesture.referenceDeltas[id].dx = 0;
4601 mPointerGesture.referenceDeltas[id].dy = 0;
4602 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004603 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004604
4605 // Add delta for all fingers and calculate a common movement delta.
4606 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004607 BitSet32 commonIdBits(mLastFingerIdBits.value
4608 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004609 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4610 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004611 uint32_t id = idBits.clearFirstMarkedBit();
4612 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4613 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004614 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4615 delta.dx += cpd.x - lpd.x;
4616 delta.dy += cpd.y - lpd.y;
4617
4618 if (first) {
4619 commonDeltaX = delta.dx;
4620 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004621 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004622 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4623 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4624 }
4625 }
Jeff Brownace13b12011-03-09 17:39:48 -08004626
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004627 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4628 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4629 float dist[MAX_POINTER_ID + 1];
4630 int32_t distOverThreshold = 0;
4631 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004632 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004633 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004634 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4635 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004636 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004637 distOverThreshold += 1;
4638 }
4639 }
4640
4641 // Only transition when at least two pointers have moved further than
4642 // the minimum distance threshold.
4643 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004644 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004645 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004646#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004647 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004648 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004649#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004650 *outCancelPreviousGesture = true;
4651 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4652 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004653 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004654 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004655 uint32_t id1 = idBits.clearFirstMarkedBit();
4656 uint32_t id2 = idBits.firstMarkedBit();
4657 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4658 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4659 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4660 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4661 // There are two pointers but they are too far apart for a SWIPE,
4662 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004663#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004664 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4665 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004666#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004667 *outCancelPreviousGesture = true;
4668 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4669 } else {
4670 // There are two pointers. Wait for both pointers to start moving
4671 // before deciding whether this is a SWIPE or FREEFORM gesture.
4672 float dist1 = dist[id1];
4673 float dist2 = dist[id2];
4674 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4675 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4676 // Calculate the dot product of the displacement vectors.
4677 // When the vectors are oriented in approximately the same direction,
4678 // the angle betweeen them is near zero and the cosine of the angle
4679 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4680 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4681 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004682 float dx1 = delta1.dx * mPointerXZoomScale;
4683 float dy1 = delta1.dy * mPointerYZoomScale;
4684 float dx2 = delta2.dx * mPointerXZoomScale;
4685 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004686 float dot = dx1 * dx2 + dy1 * dy2;
4687 float cosine = dot / (dist1 * dist2); // denominator always > 0
4688 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4689 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004690#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004691 LOGD("Gestures: PRESS transitioned to SWIPE, "
4692 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4693 "cosine %0.3f >= %0.3f",
4694 dist1, mConfig.pointerGestureMultitouchMinDistance,
4695 dist2, mConfig.pointerGestureMultitouchMinDistance,
4696 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004697#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004698 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4699 } else {
4700 // Pointers are moving in different directions. Switch to FREEFORM.
4701#if DEBUG_GESTURES
4702 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4703 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4704 "cosine %0.3f < %0.3f",
4705 dist1, mConfig.pointerGestureMultitouchMinDistance,
4706 dist2, mConfig.pointerGestureMultitouchMinDistance,
4707 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4708#endif
4709 *outCancelPreviousGesture = true;
4710 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4711 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004712 }
Jeff Brownace13b12011-03-09 17:39:48 -08004713 }
4714 }
Jeff Brownace13b12011-03-09 17:39:48 -08004715 }
4716 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004717 // Switch from SWIPE to FREEFORM if additional pointers go down.
4718 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004719 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004720#if DEBUG_GESTURES
4721 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004722 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004723#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004724 *outCancelPreviousGesture = true;
4725 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004726 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004727 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004728
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004729 // Move the reference points based on the overall group motion of the fingers
4730 // except in PRESS mode while waiting for a transition to occur.
4731 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4732 && (commonDeltaX || commonDeltaY)) {
4733 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004734 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004735 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004736 delta.dx = 0;
4737 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004738 }
4739
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004740 mPointerGesture.referenceTouchX += commonDeltaX;
4741 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004742
Jeff Brown65fd2512011-08-18 11:20:58 -07004743 commonDeltaX *= mPointerXMovementScale;
4744 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004745
Jeff Brownbe1aa822011-07-27 16:04:54 -07004746 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004747 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004748
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004749 mPointerGesture.referenceGestureX += commonDeltaX;
4750 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004751 }
4752
4753 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004754 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4755 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4756 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004757#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004758 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004759 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004760 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004761#endif
4762 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4763
4764 mPointerGesture.currentGestureIdBits.clear();
4765 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4766 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004767 mPointerGesture.currentGestureProperties[0].clear();
4768 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4769 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004770 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004771 mPointerGesture.currentGestureCoords[0].clear();
4772 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4773 mPointerGesture.referenceGestureX);
4774 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4775 mPointerGesture.referenceGestureY);
4776 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004777 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4778 // FREEFORM mode.
4779#if DEBUG_GESTURES
4780 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4781 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004782 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004783#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004784 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004785
Jeff Brownace13b12011-03-09 17:39:48 -08004786 mPointerGesture.currentGestureIdBits.clear();
4787
4788 BitSet32 mappedTouchIdBits;
4789 BitSet32 usedGestureIdBits;
4790 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4791 // Initially, assign the active gesture id to the active touch point
4792 // if there is one. No other touch id bits are mapped yet.
4793 if (!*outCancelPreviousGesture) {
4794 mappedTouchIdBits.markBit(activeTouchId);
4795 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4796 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4797 mPointerGesture.activeGestureId;
4798 } else {
4799 mPointerGesture.activeGestureId = -1;
4800 }
4801 } else {
4802 // Otherwise, assume we mapped all touches from the previous frame.
4803 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004804 mappedTouchIdBits.value = mLastFingerIdBits.value
4805 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004806 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4807
4808 // Check whether we need to choose a new active gesture id because the
4809 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004810 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4811 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004812 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004813 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004814 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4815 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4816 mPointerGesture.activeGestureId = -1;
4817 break;
4818 }
4819 }
4820 }
4821
4822#if DEBUG_GESTURES
4823 LOGD("Gestures: FREEFORM follow up "
4824 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4825 "activeGestureId=%d",
4826 mappedTouchIdBits.value, usedGestureIdBits.value,
4827 mPointerGesture.activeGestureId);
4828#endif
4829
Jeff Brown65fd2512011-08-18 11:20:58 -07004830 BitSet32 idBits(mCurrentFingerIdBits);
4831 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004832 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004833 uint32_t gestureId;
4834 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004835 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004836 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4837#if DEBUG_GESTURES
4838 LOGD("Gestures: FREEFORM "
4839 "new mapping for touch id %d -> gesture id %d",
4840 touchId, gestureId);
4841#endif
4842 } else {
4843 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4844#if DEBUG_GESTURES
4845 LOGD("Gestures: FREEFORM "
4846 "existing mapping for touch id %d -> gesture id %d",
4847 touchId, gestureId);
4848#endif
4849 }
4850 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4851 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4852
Jeff Brownbe1aa822011-07-27 16:04:54 -07004853 const RawPointerData::Pointer& pointer =
4854 mCurrentRawPointerData.pointerForId(touchId);
4855 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004856 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004857 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004858 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004859 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004860
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004861 mPointerGesture.currentGestureProperties[i].clear();
4862 mPointerGesture.currentGestureProperties[i].id = gestureId;
4863 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004864 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004865 mPointerGesture.currentGestureCoords[i].clear();
4866 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004867 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004868 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004869 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004870 mPointerGesture.currentGestureCoords[i].setAxisValue(
4871 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4872 }
4873
4874 if (mPointerGesture.activeGestureId < 0) {
4875 mPointerGesture.activeGestureId =
4876 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4877#if DEBUG_GESTURES
4878 LOGD("Gestures: FREEFORM new "
4879 "activeGestureId=%d", mPointerGesture.activeGestureId);
4880#endif
4881 }
Jeff Brown2352b972011-04-12 22:39:53 -07004882 }
Jeff Brownace13b12011-03-09 17:39:48 -08004883 }
4884
Jeff Brownbe1aa822011-07-27 16:04:54 -07004885 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004886
Jeff Brownace13b12011-03-09 17:39:48 -08004887#if DEBUG_GESTURES
4888 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004889 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4890 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004891 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004892 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4893 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004894 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004895 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004896 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004897 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004898 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004899 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4900 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4901 id, index, properties.toolType,
4902 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004903 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4904 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4905 }
4906 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004907 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004908 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004909 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004910 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004911 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4912 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4913 id, index, properties.toolType,
4914 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004915 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4916 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4917 }
4918#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004919 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004920}
4921
Jeff Brown65fd2512011-08-18 11:20:58 -07004922void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4923 mPointerSimple.currentCoords.clear();
4924 mPointerSimple.currentProperties.clear();
4925
4926 bool down, hovering;
4927 if (!mCurrentStylusIdBits.isEmpty()) {
4928 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4929 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4930 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4931 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4932 mPointerController->setPosition(x, y);
4933
4934 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4935 down = !hovering;
4936
4937 mPointerController->getPosition(&x, &y);
4938 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4939 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4940 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4941 mPointerSimple.currentProperties.id = 0;
4942 mPointerSimple.currentProperties.toolType =
4943 mCurrentCookedPointerData.pointerProperties[index].toolType;
4944 } else {
4945 down = false;
4946 hovering = false;
4947 }
4948
4949 dispatchPointerSimple(when, policyFlags, down, hovering);
4950}
4951
4952void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
4953 abortPointerSimple(when, policyFlags);
4954}
4955
4956void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
4957 mPointerSimple.currentCoords.clear();
4958 mPointerSimple.currentProperties.clear();
4959
4960 bool down, hovering;
4961 if (!mCurrentMouseIdBits.isEmpty()) {
4962 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
4963 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
4964 if (mLastMouseIdBits.hasBit(id)) {
4965 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
4966 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
4967 - mLastRawPointerData.pointers[lastIndex].x)
4968 * mPointerXMovementScale;
4969 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
4970 - mLastRawPointerData.pointers[lastIndex].y)
4971 * mPointerYMovementScale;
4972
4973 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
4974 mPointerVelocityControl.move(when, &deltaX, &deltaY);
4975
4976 mPointerController->move(deltaX, deltaY);
4977 } else {
4978 mPointerVelocityControl.reset();
4979 }
4980
4981 down = isPointerDown(mCurrentButtonState);
4982 hovering = !down;
4983
4984 float x, y;
4985 mPointerController->getPosition(&x, &y);
4986 mPointerSimple.currentCoords.copyFrom(
4987 mCurrentCookedPointerData.pointerCoords[currentIndex]);
4988 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4989 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4990 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4991 hovering ? 0.0f : 1.0f);
4992 mPointerSimple.currentProperties.id = 0;
4993 mPointerSimple.currentProperties.toolType =
4994 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
4995 } else {
4996 mPointerVelocityControl.reset();
4997
4998 down = false;
4999 hovering = false;
5000 }
5001
5002 dispatchPointerSimple(when, policyFlags, down, hovering);
5003}
5004
5005void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5006 abortPointerSimple(when, policyFlags);
5007
5008 mPointerVelocityControl.reset();
5009}
5010
5011void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5012 bool down, bool hovering) {
5013 int32_t metaState = getContext()->getGlobalMetaState();
5014
5015 if (mPointerController != NULL) {
5016 if (down || hovering) {
5017 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5018 mPointerController->clearSpots();
5019 mPointerController->setButtonState(mCurrentButtonState);
5020 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5021 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5022 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5023 }
5024 }
5025
5026 if (mPointerSimple.down && !down) {
5027 mPointerSimple.down = false;
5028
5029 // Send up.
5030 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5031 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5032 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5033 mOrientedXPrecision, mOrientedYPrecision,
5034 mPointerSimple.downTime);
5035 getListener()->notifyMotion(&args);
5036 }
5037
5038 if (mPointerSimple.hovering && !hovering) {
5039 mPointerSimple.hovering = false;
5040
5041 // Send hover exit.
5042 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5043 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5044 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5045 mOrientedXPrecision, mOrientedYPrecision,
5046 mPointerSimple.downTime);
5047 getListener()->notifyMotion(&args);
5048 }
5049
5050 if (down) {
5051 if (!mPointerSimple.down) {
5052 mPointerSimple.down = true;
5053 mPointerSimple.downTime = when;
5054
5055 // Send down.
5056 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5057 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5058 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5059 mOrientedXPrecision, mOrientedYPrecision,
5060 mPointerSimple.downTime);
5061 getListener()->notifyMotion(&args);
5062 }
5063
5064 // Send move.
5065 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5066 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5067 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5068 mOrientedXPrecision, mOrientedYPrecision,
5069 mPointerSimple.downTime);
5070 getListener()->notifyMotion(&args);
5071 }
5072
5073 if (hovering) {
5074 if (!mPointerSimple.hovering) {
5075 mPointerSimple.hovering = true;
5076
5077 // Send hover enter.
5078 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5079 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5080 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5081 mOrientedXPrecision, mOrientedYPrecision,
5082 mPointerSimple.downTime);
5083 getListener()->notifyMotion(&args);
5084 }
5085
5086 // Send hover move.
5087 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5088 AMOTION_EVENT_ACTION_HOVER_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 (mCurrentRawVScroll || mCurrentRawHScroll) {
5096 float vscroll = mCurrentRawVScroll;
5097 float hscroll = mCurrentRawHScroll;
5098 mWheelYVelocityControl.move(when, NULL, &vscroll);
5099 mWheelXVelocityControl.move(when, &hscroll, NULL);
5100
5101 // Send scroll.
5102 PointerCoords pointerCoords;
5103 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5104 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5105 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5106
5107 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5108 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5109 1, &mPointerSimple.currentProperties, &pointerCoords,
5110 mOrientedXPrecision, mOrientedYPrecision,
5111 mPointerSimple.downTime);
5112 getListener()->notifyMotion(&args);
5113 }
5114
5115 // Save state.
5116 if (down || hovering) {
5117 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5118 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5119 } else {
5120 mPointerSimple.reset();
5121 }
5122}
5123
5124void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5125 mPointerSimple.currentCoords.clear();
5126 mPointerSimple.currentProperties.clear();
5127
5128 dispatchPointerSimple(when, policyFlags, false, false);
5129}
5130
Jeff Brownace13b12011-03-09 17:39:48 -08005131void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005132 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5133 const PointerProperties* properties, const PointerCoords* coords,
5134 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005135 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5136 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005137 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005138 uint32_t pointerCount = 0;
5139 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005140 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005141 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005142 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005143 pointerCoords[pointerCount].copyFrom(coords[index]);
5144
5145 if (changedId >= 0 && id == uint32_t(changedId)) {
5146 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5147 }
5148
5149 pointerCount += 1;
5150 }
5151
Jeff Brownb6110c22011-04-01 16:15:13 -07005152 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005153
5154 if (changedId >= 0 && pointerCount == 1) {
5155 // Replace initial down and final up action.
5156 // We can compare the action without masking off the changed pointer index
5157 // because we know the index is 0.
5158 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5159 action = AMOTION_EVENT_ACTION_DOWN;
5160 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5161 action = AMOTION_EVENT_ACTION_UP;
5162 } else {
5163 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07005164 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005165 }
5166 }
5167
Jeff Brownbe1aa822011-07-27 16:04:54 -07005168 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005169 action, flags, metaState, buttonState, edgeFlags,
5170 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005171 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005172}
5173
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005174bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005175 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005176 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5177 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005178 bool changed = false;
5179 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005180 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005181 uint32_t inIndex = inIdToIndex[id];
5182 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005183
5184 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005185 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005186 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005187 PointerCoords& curOutCoords = outCoords[outIndex];
5188
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005189 if (curInProperties != curOutProperties) {
5190 curOutProperties.copyFrom(curInProperties);
5191 changed = true;
5192 }
5193
Jeff Brownace13b12011-03-09 17:39:48 -08005194 if (curInCoords != curOutCoords) {
5195 curOutCoords.copyFrom(curInCoords);
5196 changed = true;
5197 }
5198 }
5199 return changed;
5200}
5201
5202void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005203 if (mPointerController != NULL) {
5204 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5205 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005206}
5207
Jeff Brownbe1aa822011-07-27 16:04:54 -07005208bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5209 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5210 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005211}
5212
Jeff Brownbe1aa822011-07-27 16:04:54 -07005213const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005214 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005215 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005216 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005217 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005218
5219#if DEBUG_VIRTUAL_KEYS
5220 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
5221 "left=%d, top=%d, right=%d, bottom=%d",
5222 x, y,
5223 virtualKey.keyCode, virtualKey.scanCode,
5224 virtualKey.hitLeft, virtualKey.hitTop,
5225 virtualKey.hitRight, virtualKey.hitBottom);
5226#endif
5227
5228 if (virtualKey.isHit(x, y)) {
5229 return & virtualKey;
5230 }
5231 }
5232
5233 return NULL;
5234}
5235
Jeff Brownbe1aa822011-07-27 16:04:54 -07005236void TouchInputMapper::assignPointerIds() {
5237 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5238 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5239
5240 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005241
5242 if (currentPointerCount == 0) {
5243 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005244 return;
5245 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005246
Jeff Brownbe1aa822011-07-27 16:04:54 -07005247 if (lastPointerCount == 0) {
5248 // All pointers are new.
5249 for (uint32_t i = 0; i < currentPointerCount; i++) {
5250 uint32_t id = i;
5251 mCurrentRawPointerData.pointers[i].id = id;
5252 mCurrentRawPointerData.idToIndex[id] = i;
5253 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5254 }
5255 return;
5256 }
5257
5258 if (currentPointerCount == 1 && lastPointerCount == 1
5259 && mCurrentRawPointerData.pointers[0].toolType
5260 == mLastRawPointerData.pointers[0].toolType) {
5261 // Only one pointer and no change in count so it must have the same id as before.
5262 uint32_t id = mLastRawPointerData.pointers[0].id;
5263 mCurrentRawPointerData.pointers[0].id = id;
5264 mCurrentRawPointerData.idToIndex[id] = 0;
5265 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5266 return;
5267 }
5268
5269 // General case.
5270 // We build a heap of squared euclidean distances between current and last pointers
5271 // associated with the current and last pointer indices. Then, we find the best
5272 // match (by distance) for each current pointer.
5273 // The pointers must have the same tool type but it is possible for them to
5274 // transition from hovering to touching or vice-versa while retaining the same id.
5275 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5276
5277 uint32_t heapSize = 0;
5278 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5279 currentPointerIndex++) {
5280 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5281 lastPointerIndex++) {
5282 const RawPointerData::Pointer& currentPointer =
5283 mCurrentRawPointerData.pointers[currentPointerIndex];
5284 const RawPointerData::Pointer& lastPointer =
5285 mLastRawPointerData.pointers[lastPointerIndex];
5286 if (currentPointer.toolType == lastPointer.toolType) {
5287 int64_t deltaX = currentPointer.x - lastPointer.x;
5288 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005289
5290 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5291
5292 // Insert new element into the heap (sift up).
5293 heap[heapSize].currentPointerIndex = currentPointerIndex;
5294 heap[heapSize].lastPointerIndex = lastPointerIndex;
5295 heap[heapSize].distance = distance;
5296 heapSize += 1;
5297 }
5298 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005299 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005300
Jeff Brownbe1aa822011-07-27 16:04:54 -07005301 // Heapify
5302 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5303 startIndex -= 1;
5304 for (uint32_t parentIndex = startIndex; ;) {
5305 uint32_t childIndex = parentIndex * 2 + 1;
5306 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005307 break;
5308 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005309
5310 if (childIndex + 1 < heapSize
5311 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5312 childIndex += 1;
5313 }
5314
5315 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5316 break;
5317 }
5318
5319 swap(heap[parentIndex], heap[childIndex]);
5320 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005321 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005322 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005323
5324#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07005325 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
5326 for (size_t i = 0; i < heapSize; i++) {
5327 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5328 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5329 heap[i].distance);
5330 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005331#endif
5332
Jeff Brownbe1aa822011-07-27 16:04:54 -07005333 // Pull matches out by increasing order of distance.
5334 // To avoid reassigning pointers that have already been matched, the loop keeps track
5335 // of which last and current pointers have been matched using the matchedXXXBits variables.
5336 // It also tracks the used pointer id bits.
5337 BitSet32 matchedLastBits(0);
5338 BitSet32 matchedCurrentBits(0);
5339 BitSet32 usedIdBits(0);
5340 bool first = true;
5341 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5342 while (heapSize > 0) {
5343 if (first) {
5344 // The first time through the loop, we just consume the root element of
5345 // the heap (the one with smallest distance).
5346 first = false;
5347 } else {
5348 // Previous iterations consumed the root element of the heap.
5349 // Pop root element off of the heap (sift down).
5350 heap[0] = heap[heapSize];
5351 for (uint32_t parentIndex = 0; ;) {
5352 uint32_t childIndex = parentIndex * 2 + 1;
5353 if (childIndex >= heapSize) {
5354 break;
5355 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005356
Jeff Brownbe1aa822011-07-27 16:04:54 -07005357 if (childIndex + 1 < heapSize
5358 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5359 childIndex += 1;
5360 }
5361
5362 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5363 break;
5364 }
5365
5366 swap(heap[parentIndex], heap[childIndex]);
5367 parentIndex = childIndex;
5368 }
5369
5370#if DEBUG_POINTER_ASSIGNMENT
5371 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
5372 for (size_t i = 0; i < heapSize; i++) {
5373 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5374 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5375 heap[i].distance);
5376 }
5377#endif
5378 }
5379
5380 heapSize -= 1;
5381
5382 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5383 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5384
5385 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5386 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5387
5388 matchedCurrentBits.markBit(currentPointerIndex);
5389 matchedLastBits.markBit(lastPointerIndex);
5390
5391 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5392 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5393 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5394 mCurrentRawPointerData.markIdBit(id,
5395 mCurrentRawPointerData.isHovering(currentPointerIndex));
5396 usedIdBits.markBit(id);
5397
5398#if DEBUG_POINTER_ASSIGNMENT
5399 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
5400 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5401#endif
5402 break;
5403 }
5404 }
5405
5406 // Assign fresh ids to pointers that were not matched in the process.
5407 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5408 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5409 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5410
5411 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5412 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5413 mCurrentRawPointerData.markIdBit(id,
5414 mCurrentRawPointerData.isHovering(currentPointerIndex));
5415
5416#if DEBUG_POINTER_ASSIGNMENT
5417 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
5418 currentPointerIndex, id);
5419#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005420 }
5421}
5422
Jeff Brown6d0fec22010-07-23 21:28:06 -07005423int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005424 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5425 return AKEY_STATE_VIRTUAL;
5426 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005427
Jeff Brownbe1aa822011-07-27 16:04:54 -07005428 size_t numVirtualKeys = mVirtualKeys.size();
5429 for (size_t i = 0; i < numVirtualKeys; i++) {
5430 const VirtualKey& virtualKey = mVirtualKeys[i];
5431 if (virtualKey.keyCode == keyCode) {
5432 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005433 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005434 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005435
5436 return AKEY_STATE_UNKNOWN;
5437}
5438
5439int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005440 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5441 return AKEY_STATE_VIRTUAL;
5442 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005443
Jeff Brownbe1aa822011-07-27 16:04:54 -07005444 size_t numVirtualKeys = mVirtualKeys.size();
5445 for (size_t i = 0; i < numVirtualKeys; i++) {
5446 const VirtualKey& virtualKey = mVirtualKeys[i];
5447 if (virtualKey.scanCode == scanCode) {
5448 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005449 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005450 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005451
5452 return AKEY_STATE_UNKNOWN;
5453}
5454
5455bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5456 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005457 size_t numVirtualKeys = mVirtualKeys.size();
5458 for (size_t i = 0; i < numVirtualKeys; i++) {
5459 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005460
Jeff Brownbe1aa822011-07-27 16:04:54 -07005461 for (size_t i = 0; i < numCodes; i++) {
5462 if (virtualKey.keyCode == keyCodes[i]) {
5463 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005464 }
5465 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005466 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005467
5468 return true;
5469}
5470
5471
5472// --- SingleTouchInputMapper ---
5473
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005474SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5475 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005476}
5477
5478SingleTouchInputMapper::~SingleTouchInputMapper() {
5479}
5480
Jeff Brown65fd2512011-08-18 11:20:58 -07005481void SingleTouchInputMapper::reset(nsecs_t when) {
5482 mSingleTouchMotionAccumulator.reset(getDevice());
5483
5484 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005485}
5486
Jeff Brown6d0fec22010-07-23 21:28:06 -07005487void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005488 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005489
Jeff Brown65fd2512011-08-18 11:20:58 -07005490 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005491}
5492
Jeff Brown65fd2512011-08-18 11:20:58 -07005493void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005494 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005495 mCurrentRawPointerData.pointerCount = 1;
5496 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005497
Jeff Brown65fd2512011-08-18 11:20:58 -07005498 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5499 && (mTouchButtonAccumulator.isHovering()
5500 || (mRawPointerAxes.pressure.valid
5501 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005502 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005503
Jeff Brownbe1aa822011-07-27 16:04:54 -07005504 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005505 outPointer.id = 0;
5506 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5507 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5508 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5509 outPointer.touchMajor = 0;
5510 outPointer.touchMinor = 0;
5511 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5512 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5513 outPointer.orientation = 0;
5514 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005515 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5516 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005517 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5518 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5519 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5520 }
5521 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005522 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005523}
5524
Jeff Brownbe1aa822011-07-27 16:04:54 -07005525void SingleTouchInputMapper::configureRawPointerAxes() {
5526 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005527
Jeff Brownbe1aa822011-07-27 16:04:54 -07005528 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5529 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5530 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5531 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5532 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005533 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5534 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005535}
5536
5537
5538// --- MultiTouchInputMapper ---
5539
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005540MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005541 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005542}
5543
5544MultiTouchInputMapper::~MultiTouchInputMapper() {
5545}
5546
Jeff Brown65fd2512011-08-18 11:20:58 -07005547void MultiTouchInputMapper::reset(nsecs_t when) {
5548 mMultiTouchMotionAccumulator.reset(getDevice());
5549
Jeff Brown6894a292011-07-01 17:59:27 -07005550 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005551
Jeff Brown65fd2512011-08-18 11:20:58 -07005552 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005553}
5554
5555void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005556 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005557
Jeff Brown65fd2512011-08-18 11:20:58 -07005558 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005559}
5560
Jeff Brown65fd2512011-08-18 11:20:58 -07005561void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005562 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005563 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005564 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005565
Jeff Brown80fd47c2011-05-24 01:07:44 -07005566 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005567 const MultiTouchMotionAccumulator::Slot* inSlot =
5568 mMultiTouchMotionAccumulator.getSlot(inIndex);
5569 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005570 continue;
5571 }
5572
Jeff Brown80fd47c2011-05-24 01:07:44 -07005573 if (outCount >= MAX_POINTERS) {
5574#if DEBUG_POINTERS
5575 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5576 "ignoring the rest.",
5577 getDeviceName().string(), MAX_POINTERS);
5578#endif
5579 break; // too many fingers!
5580 }
5581
Jeff Brownbe1aa822011-07-27 16:04:54 -07005582 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005583 outPointer.x = inSlot->getX();
5584 outPointer.y = inSlot->getY();
5585 outPointer.pressure = inSlot->getPressure();
5586 outPointer.touchMajor = inSlot->getTouchMajor();
5587 outPointer.touchMinor = inSlot->getTouchMinor();
5588 outPointer.toolMajor = inSlot->getToolMajor();
5589 outPointer.toolMinor = inSlot->getToolMinor();
5590 outPointer.orientation = inSlot->getOrientation();
5591 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005592 outPointer.tiltX = 0;
5593 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005594
Jeff Brown49754db2011-07-01 17:37:58 -07005595 outPointer.toolType = inSlot->getToolType();
5596 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5597 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5598 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5599 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5600 }
Jeff Brown8d608662010-08-30 03:02:23 -07005601 }
5602
Jeff Brown65fd2512011-08-18 11:20:58 -07005603 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5604 && (mTouchButtonAccumulator.isHovering()
5605 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005606 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005607
Jeff Brown8d608662010-08-30 03:02:23 -07005608 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005609 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005610 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005611 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005612 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005613 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005614 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005615 if (mPointerTrackingIdMap[n] == trackingId) {
5616 id = n;
5617 }
5618 }
5619
5620 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005621 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005622 mPointerTrackingIdMap[id] = trackingId;
5623 }
5624 }
5625 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005626 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005627 mCurrentRawPointerData.clearIdBits();
5628 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005629 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005630 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005631 mCurrentRawPointerData.idToIndex[id] = outCount;
5632 mCurrentRawPointerData.markIdBit(id, isHovering);
5633 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005634 }
5635 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005636
Jeff Brown6d0fec22010-07-23 21:28:06 -07005637 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005638 }
5639
Jeff Brownbe1aa822011-07-27 16:04:54 -07005640 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005641 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005642
Jeff Brown65fd2512011-08-18 11:20:58 -07005643 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005644}
5645
Jeff Brownbe1aa822011-07-27 16:04:54 -07005646void MultiTouchInputMapper::configureRawPointerAxes() {
5647 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005648
Jeff Brownbe1aa822011-07-27 16:04:54 -07005649 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5650 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5651 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5652 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5653 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5654 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5655 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5656 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5657 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5658 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5659 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005660
Jeff Brownbe1aa822011-07-27 16:04:54 -07005661 if (mRawPointerAxes.trackingId.valid
5662 && mRawPointerAxes.slot.valid
5663 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5664 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005665 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005666 LOGW("MultiTouch Device %s reported %d slots but the framework "
5667 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005668 getDeviceName().string(), slotCount, MAX_SLOTS);
5669 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005670 }
Jeff Brown49754db2011-07-01 17:37:58 -07005671 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005672 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005673 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005674 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005675}
5676
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005677
Jeff Browncb1404e2011-01-15 18:14:15 -08005678// --- JoystickInputMapper ---
5679
5680JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5681 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005682}
5683
5684JoystickInputMapper::~JoystickInputMapper() {
5685}
5686
5687uint32_t JoystickInputMapper::getSources() {
5688 return AINPUT_SOURCE_JOYSTICK;
5689}
5690
5691void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5692 InputMapper::populateDeviceInfo(info);
5693
Jeff Brown6f2fba42011-02-19 01:08:02 -08005694 for (size_t i = 0; i < mAxes.size(); i++) {
5695 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005696 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5697 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005698 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005699 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5700 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005701 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005702 }
5703}
5704
5705void JoystickInputMapper::dump(String8& dump) {
5706 dump.append(INDENT2 "Joystick Input Mapper:\n");
5707
Jeff Brown6f2fba42011-02-19 01:08:02 -08005708 dump.append(INDENT3 "Axes:\n");
5709 size_t numAxes = mAxes.size();
5710 for (size_t i = 0; i < numAxes; i++) {
5711 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005712 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005713 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005714 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005715 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005716 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005717 }
Jeff Brown85297452011-03-04 13:07:49 -08005718 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5719 label = getAxisLabel(axis.axisInfo.highAxis);
5720 if (label) {
5721 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5722 } else {
5723 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5724 axis.axisInfo.splitValue);
5725 }
5726 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5727 dump.append(" (invert)");
5728 }
5729
5730 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5731 axis.min, axis.max, axis.flat, axis.fuzz);
5732 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5733 "highScale=%0.5f, highOffset=%0.5f\n",
5734 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005735 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5736 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005737 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005738 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005739 }
5740}
5741
Jeff Brown65fd2512011-08-18 11:20:58 -07005742void JoystickInputMapper::configure(nsecs_t when,
5743 const InputReaderConfiguration* config, uint32_t changes) {
5744 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005745
Jeff Brown474dcb52011-06-14 20:22:50 -07005746 if (!changes) { // first time only
5747 // Collect all axes.
5748 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5749 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005750 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005751 if (rawAxisInfo.valid) {
5752 // Map axis.
5753 AxisInfo axisInfo;
5754 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5755 if (!explicitlyMapped) {
5756 // Axis is not explicitly mapped, will choose a generic axis later.
5757 axisInfo.mode = AxisInfo::MODE_NORMAL;
5758 axisInfo.axis = -1;
5759 }
5760
5761 // Apply flat override.
5762 int32_t rawFlat = axisInfo.flatOverride < 0
5763 ? rawAxisInfo.flat : axisInfo.flatOverride;
5764
5765 // Calculate scaling factors and limits.
5766 Axis axis;
5767 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5768 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5769 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5770 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5771 scale, 0.0f, highScale, 0.0f,
5772 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5773 } else if (isCenteredAxis(axisInfo.axis)) {
5774 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5775 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5776 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5777 scale, offset, scale, offset,
5778 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5779 } else {
5780 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5781 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5782 scale, 0.0f, scale, 0.0f,
5783 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5784 }
5785
5786 // To eliminate noise while the joystick is at rest, filter out small variations
5787 // in axis values up front.
5788 axis.filter = axis.flat * 0.25f;
5789
5790 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005791 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005792 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005793
Jeff Brown474dcb52011-06-14 20:22:50 -07005794 // If there are too many axes, start dropping them.
5795 // Prefer to keep explicitly mapped axes.
5796 if (mAxes.size() > PointerCoords::MAX_AXES) {
5797 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5798 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5799 pruneAxes(true);
5800 pruneAxes(false);
5801 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005802
Jeff Brown474dcb52011-06-14 20:22:50 -07005803 // Assign generic axis ids to remaining axes.
5804 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5805 size_t numAxes = mAxes.size();
5806 for (size_t i = 0; i < numAxes; i++) {
5807 Axis& axis = mAxes.editValueAt(i);
5808 if (axis.axisInfo.axis < 0) {
5809 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5810 && haveAxis(nextGenericAxisId)) {
5811 nextGenericAxisId += 1;
5812 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005813
Jeff Brown474dcb52011-06-14 20:22:50 -07005814 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5815 axis.axisInfo.axis = nextGenericAxisId;
5816 nextGenericAxisId += 1;
5817 } else {
5818 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5819 "have already been assigned to other axes.",
5820 getDeviceName().string(), mAxes.keyAt(i));
5821 mAxes.removeItemsAt(i--);
5822 numAxes -= 1;
5823 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005824 }
5825 }
5826 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005827}
5828
Jeff Brown85297452011-03-04 13:07:49 -08005829bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005830 size_t numAxes = mAxes.size();
5831 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005832 const Axis& axis = mAxes.valueAt(i);
5833 if (axis.axisInfo.axis == axisId
5834 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5835 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005836 return true;
5837 }
5838 }
5839 return false;
5840}
Jeff Browncb1404e2011-01-15 18:14:15 -08005841
Jeff Brown6f2fba42011-02-19 01:08:02 -08005842void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5843 size_t i = mAxes.size();
5844 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5845 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5846 continue;
5847 }
5848 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5849 getDeviceName().string(), mAxes.keyAt(i));
5850 mAxes.removeItemsAt(i);
5851 }
5852}
5853
5854bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5855 switch (axis) {
5856 case AMOTION_EVENT_AXIS_X:
5857 case AMOTION_EVENT_AXIS_Y:
5858 case AMOTION_EVENT_AXIS_Z:
5859 case AMOTION_EVENT_AXIS_RX:
5860 case AMOTION_EVENT_AXIS_RY:
5861 case AMOTION_EVENT_AXIS_RZ:
5862 case AMOTION_EVENT_AXIS_HAT_X:
5863 case AMOTION_EVENT_AXIS_HAT_Y:
5864 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005865 case AMOTION_EVENT_AXIS_RUDDER:
5866 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005867 return true;
5868 default:
5869 return false;
5870 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005871}
5872
Jeff Brown65fd2512011-08-18 11:20:58 -07005873void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005874 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005875 size_t numAxes = mAxes.size();
5876 for (size_t i = 0; i < numAxes; i++) {
5877 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005878 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005879 }
5880
Jeff Brown65fd2512011-08-18 11:20:58 -07005881 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005882}
5883
5884void JoystickInputMapper::process(const RawEvent* rawEvent) {
5885 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005886 case EV_ABS: {
5887 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5888 if (index >= 0) {
5889 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005890 float newValue, highNewValue;
5891 switch (axis.axisInfo.mode) {
5892 case AxisInfo::MODE_INVERT:
5893 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5894 * axis.scale + axis.offset;
5895 highNewValue = 0.0f;
5896 break;
5897 case AxisInfo::MODE_SPLIT:
5898 if (rawEvent->value < axis.axisInfo.splitValue) {
5899 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5900 * axis.scale + axis.offset;
5901 highNewValue = 0.0f;
5902 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5903 newValue = 0.0f;
5904 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5905 * axis.highScale + axis.highOffset;
5906 } else {
5907 newValue = 0.0f;
5908 highNewValue = 0.0f;
5909 }
5910 break;
5911 default:
5912 newValue = rawEvent->value * axis.scale + axis.offset;
5913 highNewValue = 0.0f;
5914 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005915 }
Jeff Brown85297452011-03-04 13:07:49 -08005916 axis.newValue = newValue;
5917 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005918 }
5919 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005920 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005921
5922 case EV_SYN:
5923 switch (rawEvent->scanCode) {
5924 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005925 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005926 break;
5927 }
5928 break;
5929 }
5930}
5931
Jeff Brown6f2fba42011-02-19 01:08:02 -08005932void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005933 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005934 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005935 }
5936
5937 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005938 int32_t buttonState = 0;
5939
5940 PointerProperties pointerProperties;
5941 pointerProperties.clear();
5942 pointerProperties.id = 0;
5943 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005944
Jeff Brown6f2fba42011-02-19 01:08:02 -08005945 PointerCoords pointerCoords;
5946 pointerCoords.clear();
5947
5948 size_t numAxes = mAxes.size();
5949 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005950 const Axis& axis = mAxes.valueAt(i);
5951 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5952 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5953 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5954 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005955 }
5956
Jeff Brown56194eb2011-03-02 19:23:13 -08005957 // Moving a joystick axis should not wake the devide because joysticks can
5958 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5959 // button will likely wake the device.
5960 // TODO: Use the input device configuration to control this behavior more finely.
5961 uint32_t policyFlags = 0;
5962
Jeff Brownbe1aa822011-07-27 16:04:54 -07005963 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005964 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5965 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005966 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005967}
5968
Jeff Brown85297452011-03-04 13:07:49 -08005969bool JoystickInputMapper::filterAxes(bool force) {
5970 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005971 size_t numAxes = mAxes.size();
5972 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005973 Axis& axis = mAxes.editValueAt(i);
5974 if (force || hasValueChangedSignificantly(axis.filter,
5975 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5976 axis.currentValue = axis.newValue;
5977 atLeastOneSignificantChange = true;
5978 }
5979 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5980 if (force || hasValueChangedSignificantly(axis.filter,
5981 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5982 axis.highCurrentValue = axis.highNewValue;
5983 atLeastOneSignificantChange = true;
5984 }
5985 }
5986 }
5987 return atLeastOneSignificantChange;
5988}
5989
5990bool JoystickInputMapper::hasValueChangedSignificantly(
5991 float filter, float newValue, float currentValue, float min, float max) {
5992 if (newValue != currentValue) {
5993 // Filter out small changes in value unless the value is converging on the axis
5994 // bounds or center point. This is intended to reduce the amount of information
5995 // sent to applications by particularly noisy joysticks (such as PS3).
5996 if (fabs(newValue - currentValue) > filter
5997 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5998 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5999 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6000 return true;
6001 }
6002 }
6003 return false;
6004}
6005
6006bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6007 float filter, float newValue, float currentValue, float thresholdValue) {
6008 float newDistance = fabs(newValue - thresholdValue);
6009 if (newDistance < filter) {
6010 float oldDistance = fabs(currentValue - thresholdValue);
6011 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006012 return true;
6013 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006014 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006015 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006016}
6017
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006018} // namespace android