blob: 88378ef1f194d3e241b30d80d9ab140aec056c1c [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac02010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac02010-04-22 18:58:52 -070041#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080043#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac02010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac02010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac02010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd035822010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac02010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac02010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
Jeff Brown60691392011-07-15 19:08:26 -0700123static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800124 return rotateValueUsingRotationMap(keyCode, orientation,
125 keyCodeRotationMap, keyCodeRotationMapSize);
126}
127
Jeff Brown612891e2011-07-15 20:44:17 -0700128static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
129 float temp;
130 switch (orientation) {
131 case DISPLAY_ORIENTATION_90:
132 temp = *deltaX;
133 *deltaX = *deltaY;
134 *deltaY = -temp;
135 break;
136
137 case DISPLAY_ORIENTATION_180:
138 *deltaX = -*deltaX;
139 *deltaY = -*deltaY;
140 break;
141
142 case DISPLAY_ORIENTATION_270:
143 temp = *deltaX;
144 *deltaX = -*deltaY;
145 *deltaY = temp;
146 break;
147 }
148}
149
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
151 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
152}
153
Jeff Brownefd32662011-03-08 15:13:06 -0800154// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700155// button states. This determines whether the event is reported as a touch event.
156static bool isPointerDown(int32_t buttonState) {
157 return buttonState &
158 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700159 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800160}
161
Jeff Brown2352b972011-04-12 22:39:53 -0700162static float calculateCommonVector(float a, float b) {
163 if (a > 0 && b > 0) {
164 return a < b ? a : b;
165 } else if (a < 0 && b < 0) {
166 return a > b ? a : b;
167 } else {
168 return 0;
169 }
170}
171
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700172static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
173 nsecs_t when, int32_t deviceId, uint32_t source,
174 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
175 int32_t buttonState, int32_t keyCode) {
176 if (
177 (action == AKEY_EVENT_ACTION_DOWN
178 && !(lastButtonState & buttonState)
179 && (currentButtonState & buttonState))
180 || (action == AKEY_EVENT_ACTION_UP
181 && (lastButtonState & buttonState)
182 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700183 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700184 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700185 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700186 }
187}
188
189static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
190 nsecs_t when, int32_t deviceId, uint32_t source,
191 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
192 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
193 lastButtonState, currentButtonState,
194 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
198}
199
Jeff Brown46b9ac02010-04-22 18:58:52 -0700200
Jeff Brown65fd2512011-08-18 11:20:58 -0700201// --- InputReaderConfiguration ---
202
203bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external,
204 int32_t* width, int32_t* height, int32_t* orientation) const {
205 if (displayId == 0) {
206 const DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
207 if (info.width > 0 && info.height > 0) {
208 if (width) {
209 *width = info.width;
210 }
211 if (height) {
212 *height = info.height;
213 }
214 if (orientation) {
215 *orientation = info.orientation;
216 }
217 return true;
218 }
219 }
220 return false;
221}
222
223void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,
224 int32_t width, int32_t height, int32_t orientation) {
225 if (displayId == 0) {
226 DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
227 info.width = width;
228 info.height = height;
229 info.orientation = orientation;
230 }
231}
232
233
Jeff Brown46b9ac02010-04-22 18:58:52 -0700234// --- InputReader ---
235
236InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700238 const sp<InputListenerInterface>& listener) :
239 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700240 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700241 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700242 mQueuedListener = new QueuedInputListener(listener);
243
244 { // acquire lock
245 AutoMutex _l(mLock);
246
247 refreshConfigurationLocked(0);
248 updateGlobalMetaStateLocked();
249 updateInputConfigurationLocked();
250 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -0700251}
252
253InputReader::~InputReader() {
254 for (size_t i = 0; i < mDevices.size(); i++) {
255 delete mDevices.valueAt(i);
256 }
257}
258
259void InputReader::loopOnce() {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700260 int32_t timeoutMillis;
Jeff Brown474dcb52011-06-14 20:22:50 -0700261 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700262 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700263
Jeff Brownbe1aa822011-07-27 16:04:54 -0700264 uint32_t changes = mConfigurationChangesToRefresh;
265 if (changes) {
266 mConfigurationChangesToRefresh = 0;
267 refreshConfigurationLocked(changes);
268 }
269
270 timeoutMillis = -1;
271 if (mNextTimeout != LLONG_MAX) {
272 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
273 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
274 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700275 } // release lock
276
Jeff Brownb7198742011-03-18 18:14:26 -0700277 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700278
279 { // acquire lock
280 AutoMutex _l(mLock);
281
282 if (count) {
283 processEventsLocked(mEventBuffer, count);
284 }
285 if (!count || timeoutMillis == 0) {
286 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700287#if DEBUG_RAW_EVENTS
Jeff Brownbe1aa822011-07-27 16:04:54 -0700288 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700289#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700290 mNextTimeout = LLONG_MAX;
291 timeoutExpiredLocked(now);
292 }
293 } // release lock
294
295 // Flush queued events out to the listener.
296 // This must happen outside of the lock because the listener could potentially call
297 // back into the InputReader's methods, such as getScanCodeState, or become blocked
298 // on another thread similarly waiting to acquire the InputReader lock thereby
299 // resulting in a deadlock. This situation is actually quite plausible because the
300 // listener is actually the input dispatcher, which calls into the window manager,
301 // which occasionally calls into the input reader.
302 mQueuedListener->flush();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700303}
304
Jeff Brownbe1aa822011-07-27 16:04:54 -0700305void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700306 for (const RawEvent* rawEvent = rawEvents; count;) {
307 int32_t type = rawEvent->type;
308 size_t batchSize = 1;
309 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
310 int32_t deviceId = rawEvent->deviceId;
311 while (batchSize < count) {
312 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
313 || rawEvent[batchSize].deviceId != deviceId) {
314 break;
315 }
316 batchSize += 1;
317 }
318#if DEBUG_RAW_EVENTS
319 LOGD("BatchSize: %d Count: %d", batchSize, count);
320#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700321 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700322 } else {
323 switch (rawEvent->type) {
324 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700325 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700326 break;
327 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700328 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700329 break;
330 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700331 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700332 break;
333 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700334 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700335 break;
336 }
337 }
338 count -= batchSize;
339 rawEvent += batchSize;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700340 }
341}
342
Jeff Brown65fd2512011-08-18 11:20:58 -0700343void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700344 String8 name = mEventHub->getDeviceName(deviceId);
345 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
346
Jeff Brownbe1aa822011-07-27 16:04:54 -0700347 InputDevice* device = createDeviceLocked(deviceId, name, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700348 device->configure(when, &mConfig, 0);
349 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700350
Jeff Brown8d608662010-08-30 03:02:23 -0700351 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800352 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700353 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800354 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700355 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700356 }
357
Jeff Brownbe1aa822011-07-27 16:04:54 -0700358 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
359 if (deviceIndex < 0) {
360 mDevices.add(deviceId, device);
361 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700362 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
363 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700364 return;
365 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700366}
367
Jeff Brown65fd2512011-08-18 11:20:58 -0700368void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700369 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700370 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
371 if (deviceIndex >= 0) {
372 device = mDevices.valueAt(deviceIndex);
373 mDevices.removeItemsAt(deviceIndex, 1);
374 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700375 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700376 return;
377 }
378
Jeff Brown6d0fec22010-07-23 21:28:06 -0700379 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800380 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700381 device->getId(), device->getName().string());
382 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800383 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700384 device->getId(), device->getName().string(), device->getSources());
385 }
386
Jeff Brown65fd2512011-08-18 11:20:58 -0700387 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700388 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700389}
390
Jeff Brownbe1aa822011-07-27 16:04:54 -0700391InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
392 const String8& name, uint32_t classes) {
393 InputDevice* device = new InputDevice(&mContext, deviceId, name);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700394
Jeff Brown56194eb2011-03-02 19:23:13 -0800395 // External devices.
396 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
397 device->setExternal(true);
398 }
399
Jeff Brown6d0fec22010-07-23 21:28:06 -0700400 // Switch-like devices.
401 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
402 device->addMapper(new SwitchInputMapper(device));
403 }
404
405 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800406 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
408 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800409 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700410 }
411 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
412 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
413 }
414 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800415 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700416 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800417 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800418 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800419 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700420
Jeff Brownefd32662011-03-08 15:13:06 -0800421 if (keyboardSource != 0) {
422 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700423 }
424
Jeff Brown83c09682010-12-23 17:50:18 -0800425 // Cursor-like devices.
426 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
427 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700428 }
429
Jeff Brown58a2da82011-01-25 16:02:22 -0800430 // Touchscreens and touchpad devices.
431 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800432 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800433 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800434 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435 }
436
Jeff Browncb1404e2011-01-15 18:14:15 -0800437 // Joystick-like devices.
438 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
439 device->addMapper(new JoystickInputMapper(device));
440 }
441
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442 return device;
443}
444
Jeff Brownbe1aa822011-07-27 16:04:54 -0700445void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700446 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700447 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
448 if (deviceIndex < 0) {
449 LOGW("Discarding event for unknown deviceId %d.", deviceId);
450 return;
451 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700452
Jeff Brownbe1aa822011-07-27 16:04:54 -0700453 InputDevice* device = mDevices.valueAt(deviceIndex);
454 if (device->isIgnored()) {
455 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
456 return;
457 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700458
Jeff Brownbe1aa822011-07-27 16:04:54 -0700459 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700460}
461
Jeff Brownbe1aa822011-07-27 16:04:54 -0700462void InputReader::timeoutExpiredLocked(nsecs_t when) {
463 for (size_t i = 0; i < mDevices.size(); i++) {
464 InputDevice* device = mDevices.valueAt(i);
465 if (!device->isIgnored()) {
466 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700467 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700468 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700469}
470
Jeff Brownbe1aa822011-07-27 16:04:54 -0700471void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700473 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700474
475 // Update input configuration.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700476 updateInputConfigurationLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700477
478 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700479 NotifyConfigurationChangedArgs args(when);
480 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700481}
482
Jeff Brownbe1aa822011-07-27 16:04:54 -0700483void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700484 mPolicy->getReaderConfiguration(&mConfig);
485 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
486
Jeff Brown474dcb52011-06-14 20:22:50 -0700487 if (changes) {
488 LOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700489 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700490
491 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
492 mEventHub->requestReopenDevices();
493 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700494 for (size_t i = 0; i < mDevices.size(); i++) {
495 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700496 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700497 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700498 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700499 }
500}
501
Jeff Brownbe1aa822011-07-27 16:04:54 -0700502void InputReader::updateGlobalMetaStateLocked() {
503 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504
Jeff Brownbe1aa822011-07-27 16:04:54 -0700505 for (size_t i = 0; i < mDevices.size(); i++) {
506 InputDevice* device = mDevices.valueAt(i);
507 mGlobalMetaState |= device->getMetaState();
508 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700509}
510
Jeff Brownbe1aa822011-07-27 16:04:54 -0700511int32_t InputReader::getGlobalMetaStateLocked() {
512 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700513}
514
Jeff Brownbe1aa822011-07-27 16:04:54 -0700515void InputReader::updateInputConfigurationLocked() {
516 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
517 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
518 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
519 InputDeviceInfo deviceInfo;
520 for (size_t i = 0; i < mDevices.size(); i++) {
521 InputDevice* device = mDevices.valueAt(i);
522 device->getDeviceInfo(& deviceInfo);
523 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700524
Jeff Brownbe1aa822011-07-27 16:04:54 -0700525 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
526 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
527 }
528 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
529 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
530 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
531 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
532 }
533 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
534 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
535 }
536 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700537
Jeff Brownbe1aa822011-07-27 16:04:54 -0700538 mInputConfiguration.touchScreen = touchScreenConfig;
539 mInputConfiguration.keyboard = keyboardConfig;
540 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700541}
542
Jeff Brownbe1aa822011-07-27 16:04:54 -0700543void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800544 mDisableVirtualKeysTimeout = time;
545}
546
Jeff Brownbe1aa822011-07-27 16:04:54 -0700547bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800548 InputDevice* device, int32_t keyCode, int32_t scanCode) {
549 if (now < mDisableVirtualKeysTimeout) {
550 LOGI("Dropping virtual key from device %s because virtual keys are "
551 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
552 device->getName().string(),
553 (mDisableVirtualKeysTimeout - now) * 0.000001,
554 keyCode, scanCode);
555 return true;
556 } else {
557 return false;
558 }
559}
560
Jeff Brownbe1aa822011-07-27 16:04:54 -0700561void InputReader::fadePointerLocked() {
562 for (size_t i = 0; i < mDevices.size(); i++) {
563 InputDevice* device = mDevices.valueAt(i);
564 device->fadePointer();
565 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800566}
567
Jeff Brownbe1aa822011-07-27 16:04:54 -0700568void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700569 if (when < mNextTimeout) {
570 mNextTimeout = when;
571 }
572}
573
Jeff Brown6d0fec22010-07-23 21:28:06 -0700574void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700575 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700576
Jeff Brownbe1aa822011-07-27 16:04:54 -0700577 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700578}
579
580status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700581 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700582
Jeff Brownbe1aa822011-07-27 16:04:54 -0700583 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
584 if (deviceIndex < 0) {
585 return NAME_NOT_FOUND;
586 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700587
Jeff Brownbe1aa822011-07-27 16:04:54 -0700588 InputDevice* device = mDevices.valueAt(deviceIndex);
589 if (device->isIgnored()) {
590 return NAME_NOT_FOUND;
591 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700592
Jeff Brownbe1aa822011-07-27 16:04:54 -0700593 device->getDeviceInfo(outDeviceInfo);
594 return OK;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700595}
596
597void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700598 AutoMutex _l(mLock);
599
Jeff Brown6d0fec22010-07-23 21:28:06 -0700600 outDeviceIds.clear();
601
Jeff Brownbe1aa822011-07-27 16:04:54 -0700602 size_t numDevices = mDevices.size();
603 for (size_t i = 0; i < numDevices; i++) {
604 InputDevice* device = mDevices.valueAt(i);
605 if (!device->isIgnored()) {
606 outDeviceIds.add(device->getId());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700607 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700608 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700609}
610
611int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
612 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700613 AutoMutex _l(mLock);
614
615 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700616}
617
618int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
619 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700620 AutoMutex _l(mLock);
621
622 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700623}
624
625int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700626 AutoMutex _l(mLock);
627
628 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700629}
630
Jeff Brownbe1aa822011-07-27 16:04:54 -0700631int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700632 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700633 int32_t result = AKEY_STATE_UNKNOWN;
634 if (deviceId >= 0) {
635 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
636 if (deviceIndex >= 0) {
637 InputDevice* device = mDevices.valueAt(deviceIndex);
638 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
639 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700640 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700641 }
642 } else {
643 size_t numDevices = mDevices.size();
644 for (size_t i = 0; i < numDevices; i++) {
645 InputDevice* device = mDevices.valueAt(i);
646 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
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 Brownf2f48712010-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 Brown19c97d42011-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 Brownbb3fcba2011-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown49754db2011-07-01 17:37:58 -07001223}
1224
1225void TouchButtonAccumulator::clearButtons() {
1226 mBtnTouch = 0;
1227 mBtnStylus = 0;
1228 mBtnStylus2 = 0;
1229 mBtnToolFinger = 0;
1230 mBtnToolPen = 0;
1231 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001232 mBtnToolBrush = 0;
1233 mBtnToolPencil = 0;
1234 mBtnToolAirbrush = 0;
1235 mBtnToolMouse = 0;
1236 mBtnToolLens = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001237}
1238
1239void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1240 if (rawEvent->type == EV_KEY) {
1241 switch (rawEvent->scanCode) {
1242 case BTN_TOUCH:
1243 mBtnTouch = rawEvent->value;
1244 break;
1245 case BTN_STYLUS:
1246 mBtnStylus = rawEvent->value;
1247 break;
1248 case BTN_STYLUS2:
1249 mBtnStylus2 = rawEvent->value;
1250 break;
1251 case BTN_TOOL_FINGER:
1252 mBtnToolFinger = rawEvent->value;
1253 break;
1254 case BTN_TOOL_PEN:
1255 mBtnToolPen = rawEvent->value;
1256 break;
1257 case BTN_TOOL_RUBBER:
1258 mBtnToolRubber = rawEvent->value;
1259 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001260 case BTN_TOOL_BRUSH:
1261 mBtnToolBrush = rawEvent->value;
1262 break;
1263 case BTN_TOOL_PENCIL:
1264 mBtnToolPencil = rawEvent->value;
1265 break;
1266 case BTN_TOOL_AIRBRUSH:
1267 mBtnToolAirbrush = rawEvent->value;
1268 break;
1269 case BTN_TOOL_MOUSE:
1270 mBtnToolMouse = rawEvent->value;
1271 break;
1272 case BTN_TOOL_LENS:
1273 mBtnToolLens = rawEvent->value;
1274 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001275 }
1276 }
1277}
1278
1279uint32_t TouchButtonAccumulator::getButtonState() const {
1280 uint32_t result = 0;
1281 if (mBtnStylus) {
1282 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1283 }
1284 if (mBtnStylus2) {
1285 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1286 }
1287 return result;
1288}
1289
1290int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001291 if (mBtnToolMouse || mBtnToolLens) {
1292 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1293 }
Jeff Brown49754db2011-07-01 17:37:58 -07001294 if (mBtnToolRubber) {
1295 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1296 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001297 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001298 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1299 }
1300 if (mBtnToolFinger) {
1301 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1302 }
1303 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1304}
1305
Jeff Brownd87c6d52011-08-10 14:55:59 -07001306bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001307 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1308 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
1309 || mBtnToolMouse || mBtnToolLens;
Jeff Brown49754db2011-07-01 17:37:58 -07001310}
1311
1312bool TouchButtonAccumulator::isHovering() const {
1313 return mHaveBtnTouch && !mBtnTouch;
1314}
1315
1316
Jeff Brownbe1aa822011-07-27 16:04:54 -07001317// --- RawPointerAxes ---
1318
1319RawPointerAxes::RawPointerAxes() {
1320 clear();
1321}
1322
1323void RawPointerAxes::clear() {
1324 x.clear();
1325 y.clear();
1326 pressure.clear();
1327 touchMajor.clear();
1328 touchMinor.clear();
1329 toolMajor.clear();
1330 toolMinor.clear();
1331 orientation.clear();
1332 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001333 tiltX.clear();
1334 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001335 trackingId.clear();
1336 slot.clear();
1337}
1338
1339
1340// --- RawPointerData ---
1341
1342RawPointerData::RawPointerData() {
1343 clear();
1344}
1345
1346void RawPointerData::clear() {
1347 pointerCount = 0;
1348 clearIdBits();
1349}
1350
1351void RawPointerData::copyFrom(const RawPointerData& other) {
1352 pointerCount = other.pointerCount;
1353 hoveringIdBits = other.hoveringIdBits;
1354 touchingIdBits = other.touchingIdBits;
1355
1356 for (uint32_t i = 0; i < pointerCount; i++) {
1357 pointers[i] = other.pointers[i];
1358
1359 int id = pointers[i].id;
1360 idToIndex[id] = other.idToIndex[id];
1361 }
1362}
1363
1364void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1365 float x = 0, y = 0;
1366 uint32_t count = touchingIdBits.count();
1367 if (count) {
1368 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1369 uint32_t id = idBits.clearFirstMarkedBit();
1370 const Pointer& pointer = pointerForId(id);
1371 x += pointer.x;
1372 y += pointer.y;
1373 }
1374 x /= count;
1375 y /= count;
1376 }
1377 *outX = x;
1378 *outY = y;
1379}
1380
1381
1382// --- CookedPointerData ---
1383
1384CookedPointerData::CookedPointerData() {
1385 clear();
1386}
1387
1388void CookedPointerData::clear() {
1389 pointerCount = 0;
1390 hoveringIdBits.clear();
1391 touchingIdBits.clear();
1392}
1393
1394void CookedPointerData::copyFrom(const CookedPointerData& other) {
1395 pointerCount = other.pointerCount;
1396 hoveringIdBits = other.hoveringIdBits;
1397 touchingIdBits = other.touchingIdBits;
1398
1399 for (uint32_t i = 0; i < pointerCount; i++) {
1400 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1401 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1402
1403 int id = pointerProperties[i].id;
1404 idToIndex[id] = other.idToIndex[id];
1405 }
1406}
1407
1408
Jeff Brown49754db2011-07-01 17:37:58 -07001409// --- SingleTouchMotionAccumulator ---
1410
1411SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1412 clearAbsoluteAxes();
1413}
1414
Jeff Brown65fd2512011-08-18 11:20:58 -07001415void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1416 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1417 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1418 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1419 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1420 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1421 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1422 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1423}
1424
Jeff Brown49754db2011-07-01 17:37:58 -07001425void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1426 mAbsX = 0;
1427 mAbsY = 0;
1428 mAbsPressure = 0;
1429 mAbsToolWidth = 0;
1430 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001431 mAbsTiltX = 0;
1432 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001433}
1434
1435void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1436 if (rawEvent->type == EV_ABS) {
1437 switch (rawEvent->scanCode) {
1438 case ABS_X:
1439 mAbsX = rawEvent->value;
1440 break;
1441 case ABS_Y:
1442 mAbsY = rawEvent->value;
1443 break;
1444 case ABS_PRESSURE:
1445 mAbsPressure = rawEvent->value;
1446 break;
1447 case ABS_TOOL_WIDTH:
1448 mAbsToolWidth = rawEvent->value;
1449 break;
1450 case ABS_DISTANCE:
1451 mAbsDistance = rawEvent->value;
1452 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001453 case ABS_TILT_X:
1454 mAbsTiltX = rawEvent->value;
1455 break;
1456 case ABS_TILT_Y:
1457 mAbsTiltY = rawEvent->value;
1458 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001459 }
1460 }
1461}
1462
1463
1464// --- MultiTouchMotionAccumulator ---
1465
1466MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1467 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1468}
1469
1470MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1471 delete[] mSlots;
1472}
1473
1474void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1475 mSlotCount = slotCount;
1476 mUsingSlotsProtocol = usingSlotsProtocol;
1477
1478 delete[] mSlots;
1479 mSlots = new Slot[slotCount];
1480}
1481
Jeff Brown65fd2512011-08-18 11:20:58 -07001482void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1483 // Unfortunately there is no way to read the initial contents of the slots.
1484 // So when we reset the accumulator, we must assume they are all zeroes.
1485 if (mUsingSlotsProtocol) {
1486 // Query the driver for the current slot index and use it as the initial slot
1487 // before we start reading events from the device. It is possible that the
1488 // current slot index will not be the same as it was when the first event was
1489 // written into the evdev buffer, which means the input mapper could start
1490 // out of sync with the initial state of the events in the evdev buffer.
1491 // In the extremely unlikely case that this happens, the data from
1492 // two slots will be confused until the next ABS_MT_SLOT event is received.
1493 // This can cause the touch point to "jump", but at least there will be
1494 // no stuck touches.
1495 int32_t initialSlot;
1496 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1497 ABS_MT_SLOT, &initialSlot);
1498 if (status) {
1499 LOGD("Could not retrieve current multitouch slot index. status=%d", status);
1500 initialSlot = -1;
1501 }
1502 clearSlots(initialSlot);
1503 } else {
1504 clearSlots(-1);
1505 }
1506}
1507
Jeff Brown49754db2011-07-01 17:37:58 -07001508void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001509 if (mSlots) {
1510 for (size_t i = 0; i < mSlotCount; i++) {
1511 mSlots[i].clear();
1512 }
Jeff Brown49754db2011-07-01 17:37:58 -07001513 }
1514 mCurrentSlot = initialSlot;
1515}
1516
1517void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1518 if (rawEvent->type == EV_ABS) {
1519 bool newSlot = false;
1520 if (mUsingSlotsProtocol) {
1521 if (rawEvent->scanCode == ABS_MT_SLOT) {
1522 mCurrentSlot = rawEvent->value;
1523 newSlot = true;
1524 }
1525 } else if (mCurrentSlot < 0) {
1526 mCurrentSlot = 0;
1527 }
1528
1529 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1530#if DEBUG_POINTERS
1531 if (newSlot) {
1532 LOGW("MultiTouch device emitted invalid slot index %d but it "
1533 "should be between 0 and %d; ignoring this slot.",
1534 mCurrentSlot, mSlotCount - 1);
1535 }
1536#endif
1537 } else {
1538 Slot* slot = &mSlots[mCurrentSlot];
1539
1540 switch (rawEvent->scanCode) {
1541 case ABS_MT_POSITION_X:
1542 slot->mInUse = true;
1543 slot->mAbsMTPositionX = rawEvent->value;
1544 break;
1545 case ABS_MT_POSITION_Y:
1546 slot->mInUse = true;
1547 slot->mAbsMTPositionY = rawEvent->value;
1548 break;
1549 case ABS_MT_TOUCH_MAJOR:
1550 slot->mInUse = true;
1551 slot->mAbsMTTouchMajor = rawEvent->value;
1552 break;
1553 case ABS_MT_TOUCH_MINOR:
1554 slot->mInUse = true;
1555 slot->mAbsMTTouchMinor = rawEvent->value;
1556 slot->mHaveAbsMTTouchMinor = true;
1557 break;
1558 case ABS_MT_WIDTH_MAJOR:
1559 slot->mInUse = true;
1560 slot->mAbsMTWidthMajor = rawEvent->value;
1561 break;
1562 case ABS_MT_WIDTH_MINOR:
1563 slot->mInUse = true;
1564 slot->mAbsMTWidthMinor = rawEvent->value;
1565 slot->mHaveAbsMTWidthMinor = true;
1566 break;
1567 case ABS_MT_ORIENTATION:
1568 slot->mInUse = true;
1569 slot->mAbsMTOrientation = rawEvent->value;
1570 break;
1571 case ABS_MT_TRACKING_ID:
1572 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001573 // The slot is no longer in use but it retains its previous contents,
1574 // which may be reused for subsequent touches.
1575 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001576 } else {
1577 slot->mInUse = true;
1578 slot->mAbsMTTrackingId = rawEvent->value;
1579 }
1580 break;
1581 case ABS_MT_PRESSURE:
1582 slot->mInUse = true;
1583 slot->mAbsMTPressure = rawEvent->value;
1584 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001585 case ABS_MT_DISTANCE:
1586 slot->mInUse = true;
1587 slot->mAbsMTDistance = rawEvent->value;
1588 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001589 case ABS_MT_TOOL_TYPE:
1590 slot->mInUse = true;
1591 slot->mAbsMTToolType = rawEvent->value;
1592 slot->mHaveAbsMTToolType = true;
1593 break;
1594 }
1595 }
1596 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1597 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1598 mCurrentSlot += 1;
1599 }
1600}
1601
Jeff Brown65fd2512011-08-18 11:20:58 -07001602void MultiTouchMotionAccumulator::finishSync() {
1603 if (!mUsingSlotsProtocol) {
1604 clearSlots(-1);
1605 }
1606}
1607
Jeff Brown49754db2011-07-01 17:37:58 -07001608
1609// --- MultiTouchMotionAccumulator::Slot ---
1610
1611MultiTouchMotionAccumulator::Slot::Slot() {
1612 clear();
1613}
1614
Jeff Brown49754db2011-07-01 17:37:58 -07001615void MultiTouchMotionAccumulator::Slot::clear() {
1616 mInUse = false;
1617 mHaveAbsMTTouchMinor = false;
1618 mHaveAbsMTWidthMinor = false;
1619 mHaveAbsMTToolType = false;
1620 mAbsMTPositionX = 0;
1621 mAbsMTPositionY = 0;
1622 mAbsMTTouchMajor = 0;
1623 mAbsMTTouchMinor = 0;
1624 mAbsMTWidthMajor = 0;
1625 mAbsMTWidthMinor = 0;
1626 mAbsMTOrientation = 0;
1627 mAbsMTTrackingId = -1;
1628 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001629 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001630 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001631}
1632
1633int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1634 if (mHaveAbsMTToolType) {
1635 switch (mAbsMTToolType) {
1636 case MT_TOOL_FINGER:
1637 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1638 case MT_TOOL_PEN:
1639 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1640 }
1641 }
1642 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1643}
1644
1645
Jeff Brown6d0fec22010-07-23 21:28:06 -07001646// --- InputMapper ---
1647
1648InputMapper::InputMapper(InputDevice* device) :
1649 mDevice(device), mContext(device->getContext()) {
1650}
1651
1652InputMapper::~InputMapper() {
1653}
1654
1655void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1656 info->addSource(getSources());
1657}
1658
Jeff Brownef3d7e82010-09-30 14:33:04 -07001659void InputMapper::dump(String8& dump) {
1660}
1661
Jeff Brown65fd2512011-08-18 11:20:58 -07001662void InputMapper::configure(nsecs_t when,
1663 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001664}
1665
Jeff Brown65fd2512011-08-18 11:20:58 -07001666void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001667}
1668
Jeff Brownaa3855d2011-03-17 01:34:19 -07001669void InputMapper::timeoutExpired(nsecs_t when) {
1670}
1671
Jeff Brown6d0fec22010-07-23 21:28:06 -07001672int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1673 return AKEY_STATE_UNKNOWN;
1674}
1675
1676int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1677 return AKEY_STATE_UNKNOWN;
1678}
1679
1680int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1681 return AKEY_STATE_UNKNOWN;
1682}
1683
1684bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1685 const int32_t* keyCodes, uint8_t* outFlags) {
1686 return false;
1687}
1688
1689int32_t InputMapper::getMetaState() {
1690 return 0;
1691}
1692
Jeff Brown05dc66a2011-03-02 14:41:58 -08001693void InputMapper::fadePointer() {
1694}
1695
Jeff Brownbe1aa822011-07-27 16:04:54 -07001696status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1697 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1698}
1699
Jeff Browncb1404e2011-01-15 18:14:15 -08001700void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1701 const RawAbsoluteAxisInfo& axis, const char* name) {
1702 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001703 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1704 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001705 } else {
1706 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1707 }
1708}
1709
Jeff Brown6d0fec22010-07-23 21:28:06 -07001710
1711// --- SwitchInputMapper ---
1712
1713SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1714 InputMapper(device) {
1715}
1716
1717SwitchInputMapper::~SwitchInputMapper() {
1718}
1719
1720uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001721 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001722}
1723
1724void SwitchInputMapper::process(const RawEvent* rawEvent) {
1725 switch (rawEvent->type) {
1726 case EV_SW:
1727 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1728 break;
1729 }
1730}
1731
1732void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001733 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1734 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001735}
1736
1737int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1738 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1739}
1740
1741
1742// --- KeyboardInputMapper ---
1743
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001744KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001745 uint32_t source, int32_t keyboardType) :
1746 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001747 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001748}
1749
1750KeyboardInputMapper::~KeyboardInputMapper() {
1751}
1752
Jeff Brown6d0fec22010-07-23 21:28:06 -07001753uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001754 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001755}
1756
1757void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1758 InputMapper::populateDeviceInfo(info);
1759
1760 info->setKeyboardType(mKeyboardType);
1761}
1762
Jeff Brownef3d7e82010-09-30 14:33:04 -07001763void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001764 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1765 dumpParameters(dump);
1766 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001767 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001768 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1769 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1770 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001771}
1772
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001773
Jeff Brown65fd2512011-08-18 11:20:58 -07001774void KeyboardInputMapper::configure(nsecs_t when,
1775 const InputReaderConfiguration* config, uint32_t changes) {
1776 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001777
Jeff Brown474dcb52011-06-14 20:22:50 -07001778 if (!changes) { // first time only
1779 // Configure basic parameters.
1780 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07001781 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001782
Jeff Brown65fd2512011-08-18 11:20:58 -07001783 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1784 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1785 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
1786 false /*external*/, NULL, NULL, &mOrientation)) {
1787 mOrientation = DISPLAY_ORIENTATION_0;
1788 }
1789 } else {
1790 mOrientation = DISPLAY_ORIENTATION_0;
1791 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001792 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001793}
1794
1795void KeyboardInputMapper::configureParameters() {
1796 mParameters.orientationAware = false;
1797 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1798 mParameters.orientationAware);
1799
Jeff Brownbc68a592011-07-25 12:58:12 -07001800 mParameters.associatedDisplayId = -1;
1801 if (mParameters.orientationAware) {
1802 mParameters.associatedDisplayId = 0;
1803 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001804}
1805
1806void KeyboardInputMapper::dumpParameters(String8& dump) {
1807 dump.append(INDENT3 "Parameters:\n");
1808 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1809 mParameters.associatedDisplayId);
1810 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1811 toString(mParameters.orientationAware));
1812}
1813
Jeff Brown65fd2512011-08-18 11:20:58 -07001814void KeyboardInputMapper::reset(nsecs_t when) {
1815 mMetaState = AMETA_NONE;
1816 mDownTime = 0;
1817 mKeyDowns.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001818
Jeff Brownbe1aa822011-07-27 16:04:54 -07001819 resetLedState();
1820
Jeff Brown65fd2512011-08-18 11:20:58 -07001821 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001822}
1823
1824void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1825 switch (rawEvent->type) {
1826 case EV_KEY: {
1827 int32_t scanCode = rawEvent->scanCode;
1828 if (isKeyboardOrGamepadKey(scanCode)) {
1829 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1830 rawEvent->flags);
1831 }
1832 break;
1833 }
1834 }
1835}
1836
1837bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1838 return scanCode < BTN_MOUSE
1839 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001840 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001841 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001842}
1843
Jeff Brown6328cdc2010-07-29 18:18:33 -07001844void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1845 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001846
Jeff Brownbe1aa822011-07-27 16:04:54 -07001847 if (down) {
1848 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001849 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001850 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001851 }
Jeff Brownfe508922011-01-18 15:10:10 -08001852
Jeff Brownbe1aa822011-07-27 16:04:54 -07001853 // Add key down.
1854 ssize_t keyDownIndex = findKeyDown(scanCode);
1855 if (keyDownIndex >= 0) {
1856 // key repeat, be sure to use same keycode as before in case of rotation
1857 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001858 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001859 // key down
1860 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1861 && mContext->shouldDropVirtualKey(when,
1862 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001863 return;
1864 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001865
1866 mKeyDowns.push();
1867 KeyDown& keyDown = mKeyDowns.editTop();
1868 keyDown.keyCode = keyCode;
1869 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001870 }
1871
Jeff Brownbe1aa822011-07-27 16:04:54 -07001872 mDownTime = when;
1873 } else {
1874 // Remove key down.
1875 ssize_t keyDownIndex = findKeyDown(scanCode);
1876 if (keyDownIndex >= 0) {
1877 // key up, be sure to use same keycode as before in case of rotation
1878 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1879 mKeyDowns.removeAt(size_t(keyDownIndex));
1880 } else {
1881 // key was not actually down
1882 LOGI("Dropping key up from device %s because the key was not down. "
1883 "keyCode=%d, scanCode=%d",
1884 getDeviceName().string(), keyCode, scanCode);
1885 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001886 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001887 }
Jeff Brownfd035822010-06-30 16:10:35 -07001888
Jeff Brownbe1aa822011-07-27 16:04:54 -07001889 bool metaStateChanged = false;
1890 int32_t oldMetaState = mMetaState;
1891 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1892 if (oldMetaState != newMetaState) {
1893 mMetaState = newMetaState;
1894 metaStateChanged = true;
1895 updateLedState(false);
1896 }
1897
1898 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001899
Jeff Brown56194eb2011-03-02 19:23:13 -08001900 // Key down on external an keyboard should wake the device.
1901 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1902 // For internal keyboards, the key layout file should specify the policy flags for
1903 // each wake key individually.
1904 // TODO: Use the input device configuration to control this behavior more finely.
1905 if (down && getDevice()->isExternal()
1906 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1907 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1908 }
1909
Jeff Brown6328cdc2010-07-29 18:18:33 -07001910 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001911 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001912 }
1913
Jeff Brown05dc66a2011-03-02 14:41:58 -08001914 if (down && !isMetaKey(keyCode)) {
1915 getContext()->fadePointer();
1916 }
1917
Jeff Brownbe1aa822011-07-27 16:04:54 -07001918 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001919 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1920 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001921 getListener()->notifyKey(&args);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001922}
1923
Jeff Brownbe1aa822011-07-27 16:04:54 -07001924ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1925 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001926 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001927 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001928 return i;
1929 }
1930 }
1931 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001932}
1933
Jeff Brown6d0fec22010-07-23 21:28:06 -07001934int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1935 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1936}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001937
Jeff Brown6d0fec22010-07-23 21:28:06 -07001938int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1939 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1940}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001941
Jeff Brown6d0fec22010-07-23 21:28:06 -07001942bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1943 const int32_t* keyCodes, uint8_t* outFlags) {
1944 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1945}
1946
1947int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001948 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001949}
1950
Jeff Brownbe1aa822011-07-27 16:04:54 -07001951void KeyboardInputMapper::resetLedState() {
1952 initializeLedState(mCapsLockLedState, LED_CAPSL);
1953 initializeLedState(mNumLockLedState, LED_NUML);
1954 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001955
Jeff Brownbe1aa822011-07-27 16:04:54 -07001956 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001957}
1958
Jeff Brownbe1aa822011-07-27 16:04:54 -07001959void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001960 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1961 ledState.on = false;
1962}
1963
Jeff Brownbe1aa822011-07-27 16:04:54 -07001964void KeyboardInputMapper::updateLedState(bool reset) {
1965 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001966 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001967 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001968 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001969 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001970 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001971}
1972
Jeff Brownbe1aa822011-07-27 16:04:54 -07001973void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07001974 int32_t led, int32_t modifier, bool reset) {
1975 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001976 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001977 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001978 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1979 ledState.on = desiredState;
1980 }
1981 }
1982}
1983
Jeff Brown6d0fec22010-07-23 21:28:06 -07001984
Jeff Brown83c09682010-12-23 17:50:18 -08001985// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001986
Jeff Brown83c09682010-12-23 17:50:18 -08001987CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001988 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001989}
1990
Jeff Brown83c09682010-12-23 17:50:18 -08001991CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001992}
1993
Jeff Brown83c09682010-12-23 17:50:18 -08001994uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001995 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001996}
1997
Jeff Brown83c09682010-12-23 17:50:18 -08001998void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001999 InputMapper::populateDeviceInfo(info);
2000
Jeff Brown83c09682010-12-23 17:50:18 -08002001 if (mParameters.mode == Parameters::MODE_POINTER) {
2002 float minX, minY, maxX, maxY;
2003 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002004 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2005 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002006 }
2007 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002008 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2009 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002010 }
Jeff Brownefd32662011-03-08 15:13:06 -08002011 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002012
Jeff Brown65fd2512011-08-18 11:20:58 -07002013 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002014 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002015 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002016 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002017 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002018 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002019}
2020
Jeff Brown83c09682010-12-23 17:50:18 -08002021void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002022 dump.append(INDENT2 "Cursor Input Mapper:\n");
2023 dumpParameters(dump);
2024 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2025 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2026 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2027 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2028 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002029 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002030 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002031 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002032 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2033 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002034 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002035 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2036 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2037 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002038}
2039
Jeff Brown65fd2512011-08-18 11:20:58 -07002040void CursorInputMapper::configure(nsecs_t when,
2041 const InputReaderConfiguration* config, uint32_t changes) {
2042 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002043
Jeff Brown474dcb52011-06-14 20:22:50 -07002044 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002045 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002046
Jeff Brown474dcb52011-06-14 20:22:50 -07002047 // Configure basic parameters.
2048 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002049
Jeff Brown474dcb52011-06-14 20:22:50 -07002050 // Configure device mode.
2051 switch (mParameters.mode) {
2052 case Parameters::MODE_POINTER:
2053 mSource = AINPUT_SOURCE_MOUSE;
2054 mXPrecision = 1.0f;
2055 mYPrecision = 1.0f;
2056 mXScale = 1.0f;
2057 mYScale = 1.0f;
2058 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2059 break;
2060 case Parameters::MODE_NAVIGATION:
2061 mSource = AINPUT_SOURCE_TRACKBALL;
2062 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2063 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2064 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2065 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2066 break;
2067 }
2068
2069 mVWheelScale = 1.0f;
2070 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002071 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002072
Jeff Brown474dcb52011-06-14 20:22:50 -07002073 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2074 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2075 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2076 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2077 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002078
2079 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2080 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2081 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2082 false /*external*/, NULL, NULL, &mOrientation)) {
2083 mOrientation = DISPLAY_ORIENTATION_0;
2084 }
2085 } else {
2086 mOrientation = DISPLAY_ORIENTATION_0;
2087 }
2088 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002089}
2090
Jeff Brown83c09682010-12-23 17:50:18 -08002091void CursorInputMapper::configureParameters() {
2092 mParameters.mode = Parameters::MODE_POINTER;
2093 String8 cursorModeString;
2094 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2095 if (cursorModeString == "navigation") {
2096 mParameters.mode = Parameters::MODE_NAVIGATION;
2097 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
2098 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
2099 }
2100 }
2101
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002102 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002103 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002104 mParameters.orientationAware);
2105
Jeff Brownbc68a592011-07-25 12:58:12 -07002106 mParameters.associatedDisplayId = -1;
2107 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2108 mParameters.associatedDisplayId = 0;
2109 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002110}
2111
Jeff Brown83c09682010-12-23 17:50:18 -08002112void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002113 dump.append(INDENT3 "Parameters:\n");
2114 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2115 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002116
2117 switch (mParameters.mode) {
2118 case Parameters::MODE_POINTER:
2119 dump.append(INDENT4 "Mode: pointer\n");
2120 break;
2121 case Parameters::MODE_NAVIGATION:
2122 dump.append(INDENT4 "Mode: navigation\n");
2123 break;
2124 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002125 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002126 }
2127
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002128 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2129 toString(mParameters.orientationAware));
2130}
2131
Jeff Brown65fd2512011-08-18 11:20:58 -07002132void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002133 mButtonState = 0;
2134 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002135
Jeff Brownbe1aa822011-07-27 16:04:54 -07002136 mPointerVelocityControl.reset();
2137 mWheelXVelocityControl.reset();
2138 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002139
Jeff Brown65fd2512011-08-18 11:20:58 -07002140 mCursorButtonAccumulator.reset(getDevice());
2141 mCursorMotionAccumulator.reset(getDevice());
2142 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002143
Jeff Brown65fd2512011-08-18 11:20:58 -07002144 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002145}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002146
Jeff Brown83c09682010-12-23 17:50:18 -08002147void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002148 mCursorButtonAccumulator.process(rawEvent);
2149 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002150 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002151
Jeff Brown49754db2011-07-01 17:37:58 -07002152 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
2153 sync(rawEvent->when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002154 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002155}
2156
Jeff Brown83c09682010-12-23 17:50:18 -08002157void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002158 int32_t lastButtonState = mButtonState;
2159 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2160 mButtonState = currentButtonState;
2161
2162 bool wasDown = isPointerDown(lastButtonState);
2163 bool down = isPointerDown(currentButtonState);
2164 bool downChanged;
2165 if (!wasDown && down) {
2166 mDownTime = when;
2167 downChanged = true;
2168 } else if (wasDown && !down) {
2169 downChanged = true;
2170 } else {
2171 downChanged = false;
2172 }
2173 nsecs_t downTime = mDownTime;
2174 bool buttonsChanged = currentButtonState != lastButtonState;
2175
2176 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2177 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2178 bool moved = deltaX != 0 || deltaY != 0;
2179
Jeff Brown65fd2512011-08-18 11:20:58 -07002180 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002181 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2182 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002183 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002184 }
2185
Jeff Brown65fd2512011-08-18 11:20:58 -07002186 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002187 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002188 pointerProperties.clear();
2189 pointerProperties.id = 0;
2190 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2191
Jeff Brown6328cdc2010-07-29 18:18:33 -07002192 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002193 pointerCoords.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002194
Jeff Brown65fd2512011-08-18 11:20:58 -07002195 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2196 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002197 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002198
Jeff Brownbe1aa822011-07-27 16:04:54 -07002199 mWheelYVelocityControl.move(when, NULL, &vscroll);
2200 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002201
Jeff Brownbe1aa822011-07-27 16:04:54 -07002202 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002203
Jeff Brownbe1aa822011-07-27 16:04:54 -07002204 if (mPointerController != NULL) {
2205 if (moved || scrolled || buttonsChanged) {
2206 mPointerController->setPresentation(
2207 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002208
Jeff Brownbe1aa822011-07-27 16:04:54 -07002209 if (moved) {
2210 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002211 }
2212
Jeff Brownbe1aa822011-07-27 16:04:54 -07002213 if (buttonsChanged) {
2214 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002215 }
Jeff Brownefd32662011-03-08 15:13:06 -08002216
Jeff Brownbe1aa822011-07-27 16:04:54 -07002217 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002218 }
2219
Jeff Brownbe1aa822011-07-27 16:04:54 -07002220 float x, y;
2221 mPointerController->getPosition(&x, &y);
2222 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2223 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2224 } else {
2225 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2226 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2227 }
2228
2229 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002230
Jeff Brown56194eb2011-03-02 19:23:13 -08002231 // Moving an external trackball or mouse should wake the device.
2232 // We don't do this for internal cursor devices to prevent them from waking up
2233 // the device in your pocket.
2234 // TODO: Use the input device configuration to control this behavior more finely.
2235 uint32_t policyFlags = 0;
2236 if (getDevice()->isExternal()) {
2237 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2238 }
2239
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002240 // Synthesize key down from buttons if needed.
2241 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2242 policyFlags, lastButtonState, currentButtonState);
2243
2244 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002245 if (downChanged || moved || scrolled || buttonsChanged) {
2246 int32_t metaState = mContext->getGlobalMetaState();
2247 int32_t motionEventAction;
2248 if (downChanged) {
2249 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2250 } else if (down || mPointerController == NULL) {
2251 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2252 } else {
2253 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2254 }
Jeff Brownb6997262010-10-08 22:31:17 -07002255
Jeff Brownbe1aa822011-07-27 16:04:54 -07002256 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2257 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002258 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002259 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002260
Jeff Brownbe1aa822011-07-27 16:04:54 -07002261 // Send hover move after UP to tell the application that the mouse is hovering now.
2262 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2263 && mPointerController != NULL) {
2264 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2265 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2266 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2267 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2268 getListener()->notifyMotion(&hoverArgs);
2269 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002270
Jeff Brownbe1aa822011-07-27 16:04:54 -07002271 // Send scroll events.
2272 if (scrolled) {
2273 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2274 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2275
2276 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2277 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2278 AMOTION_EVENT_EDGE_FLAG_NONE,
2279 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2280 getListener()->notifyMotion(&scrollArgs);
2281 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002282 }
Jeff Browna032cc02011-03-07 16:56:21 -08002283
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002284 // Synthesize key up from buttons if needed.
2285 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2286 policyFlags, lastButtonState, currentButtonState);
2287
Jeff Brown65fd2512011-08-18 11:20:58 -07002288 mCursorMotionAccumulator.finishSync();
2289 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002290}
2291
Jeff Brown83c09682010-12-23 17:50:18 -08002292int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002293 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2294 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2295 } else {
2296 return AKEY_STATE_UNKNOWN;
2297 }
2298}
2299
Jeff Brown05dc66a2011-03-02 14:41:58 -08002300void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002301 if (mPointerController != NULL) {
2302 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2303 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002304}
2305
Jeff Brown6d0fec22010-07-23 21:28:06 -07002306
2307// --- TouchInputMapper ---
2308
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002309TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002310 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002311 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002312 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002313}
2314
2315TouchInputMapper::~TouchInputMapper() {
2316}
2317
2318uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002319 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002320}
2321
2322void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2323 InputMapper::populateDeviceInfo(info);
2324
Jeff Brown65fd2512011-08-18 11:20:58 -07002325 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2326 info->addMotionRange(mOrientedRanges.x);
2327 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002328 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002329
Jeff Brown65fd2512011-08-18 11:20:58 -07002330 if (mOrientedRanges.haveSize) {
2331 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002332 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002333
2334 if (mOrientedRanges.haveTouchSize) {
2335 info->addMotionRange(mOrientedRanges.touchMajor);
2336 info->addMotionRange(mOrientedRanges.touchMinor);
2337 }
2338
2339 if (mOrientedRanges.haveToolSize) {
2340 info->addMotionRange(mOrientedRanges.toolMajor);
2341 info->addMotionRange(mOrientedRanges.toolMinor);
2342 }
2343
2344 if (mOrientedRanges.haveOrientation) {
2345 info->addMotionRange(mOrientedRanges.orientation);
2346 }
2347
2348 if (mOrientedRanges.haveDistance) {
2349 info->addMotionRange(mOrientedRanges.distance);
2350 }
2351
2352 if (mOrientedRanges.haveTilt) {
2353 info->addMotionRange(mOrientedRanges.tilt);
2354 }
2355
2356 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2357 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2358 }
2359 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2360 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2361 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002362 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002363}
2364
Jeff Brownef3d7e82010-09-30 14:33:04 -07002365void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002366 dump.append(INDENT2 "Touch Input Mapper:\n");
2367 dumpParameters(dump);
2368 dumpVirtualKeys(dump);
2369 dumpRawPointerAxes(dump);
2370 dumpCalibration(dump);
2371 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002372
Jeff Brownbe1aa822011-07-27 16:04:54 -07002373 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2374 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2375 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2376 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2377 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2378 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002379 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2380 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002381 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002382 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2383 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002384 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2385 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2386 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2387 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2388 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002389
Jeff Brownbe1aa822011-07-27 16:04:54 -07002390 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002391
Jeff Brownbe1aa822011-07-27 16:04:54 -07002392 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2393 mLastRawPointerData.pointerCount);
2394 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2395 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2396 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2397 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002398 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2399 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002400 pointer.id, pointer.x, pointer.y, pointer.pressure,
2401 pointer.touchMajor, pointer.touchMinor,
2402 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002403 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002404 pointer.toolType, toString(pointer.isHovering));
2405 }
2406
2407 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2408 mLastCookedPointerData.pointerCount);
2409 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2410 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2411 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2412 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2413 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002414 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2415 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002416 pointerProperties.id,
2417 pointerCoords.getX(),
2418 pointerCoords.getY(),
2419 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2420 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2421 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2422 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2423 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2424 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002425 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002426 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2427 pointerProperties.toolType,
2428 toString(mLastCookedPointerData.isHovering(i)));
2429 }
2430
Jeff Brown65fd2512011-08-18 11:20:58 -07002431 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002432 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2433 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002434 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002435 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002436 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002437 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002438 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002439 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002440 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002441 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2442 mPointerGestureMaxSwipeWidth);
2443 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002444}
2445
Jeff Brown65fd2512011-08-18 11:20:58 -07002446void TouchInputMapper::configure(nsecs_t when,
2447 const InputReaderConfiguration* config, uint32_t changes) {
2448 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002449
Jeff Brown474dcb52011-06-14 20:22:50 -07002450 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002451
Jeff Brown474dcb52011-06-14 20:22:50 -07002452 if (!changes) { // first time only
2453 // Configure basic parameters.
2454 configureParameters();
2455
Jeff Brown65fd2512011-08-18 11:20:58 -07002456 // Configure common accumulators.
2457 mCursorScrollAccumulator.configure(getDevice());
2458 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002459
2460 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002461 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002462
2463 // Prepare input device calibration.
2464 parseCalibration();
2465 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002466 }
2467
Jeff Brown474dcb52011-06-14 20:22:50 -07002468 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002469 // Update pointer speed.
2470 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2471 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2472 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002473 }
Jeff Brown8d608662010-08-30 03:02:23 -07002474
Jeff Brown65fd2512011-08-18 11:20:58 -07002475 bool resetNeeded = false;
2476 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
2477 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT))) {
2478 // Configure device sources, surface dimensions, orientation and
2479 // scaling factors.
2480 configureSurface(when, &resetNeeded);
2481 }
2482
2483 if (changes && resetNeeded) {
2484 // Send reset, unless this is the first time the device has been configured,
2485 // in which case the reader will call reset itself after all mappers are ready.
2486 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002487 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002488}
2489
Jeff Brown8d608662010-08-30 03:02:23 -07002490void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002491 // Use the pointer presentation mode for devices that do not support distinct
2492 // multitouch. The spot-based presentation relies on being able to accurately
2493 // locate two or more fingers on the touch pad.
2494 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2495 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002496
Jeff Brown538881e2011-05-25 18:23:38 -07002497 String8 gestureModeString;
2498 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2499 gestureModeString)) {
2500 if (gestureModeString == "pointer") {
2501 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2502 } else if (gestureModeString == "spots") {
2503 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2504 } else if (gestureModeString != "default") {
2505 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2506 }
2507 }
2508
Jeff Brownace13b12011-03-09 17:39:48 -08002509 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2510 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2511 // The device is a cursor device with a touch pad attached.
2512 // By default don't use the touch pad to move the pointer.
2513 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002514 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2515 // The device is a pointing device like a track pad.
2516 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2517 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2518 // The device is a touch screen.
2519 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002520 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002521 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002522 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2523 }
2524
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002525 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002526 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2527 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002528 if (deviceTypeString == "touchScreen") {
2529 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002530 } else if (deviceTypeString == "touchPad") {
2531 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002532 } else if (deviceTypeString == "pointer") {
2533 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002534 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002535 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2536 }
2537 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002538
Jeff Brownefd32662011-03-08 15:13:06 -08002539 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002540 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2541 mParameters.orientationAware);
2542
Jeff Brownbc68a592011-07-25 12:58:12 -07002543 mParameters.associatedDisplayId = -1;
2544 mParameters.associatedDisplayIsExternal = false;
2545 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002546 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002547 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2548 mParameters.associatedDisplayIsExternal =
2549 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2550 && getDevice()->isExternal();
2551 mParameters.associatedDisplayId = 0;
2552 }
Jeff Brown8d608662010-08-30 03:02:23 -07002553}
2554
Jeff Brownef3d7e82010-09-30 14:33:04 -07002555void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002556 dump.append(INDENT3 "Parameters:\n");
2557
Jeff Brown538881e2011-05-25 18:23:38 -07002558 switch (mParameters.gestureMode) {
2559 case Parameters::GESTURE_MODE_POINTER:
2560 dump.append(INDENT4 "GestureMode: pointer\n");
2561 break;
2562 case Parameters::GESTURE_MODE_SPOTS:
2563 dump.append(INDENT4 "GestureMode: spots\n");
2564 break;
2565 default:
2566 assert(false);
2567 }
2568
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002569 switch (mParameters.deviceType) {
2570 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2571 dump.append(INDENT4 "DeviceType: touchScreen\n");
2572 break;
2573 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2574 dump.append(INDENT4 "DeviceType: touchPad\n");
2575 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002576 case Parameters::DEVICE_TYPE_POINTER:
2577 dump.append(INDENT4 "DeviceType: pointer\n");
2578 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002579 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002580 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002581 }
2582
Jeff Brown65fd2512011-08-18 11:20:58 -07002583 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2584 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002585 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2586 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002587}
2588
Jeff Brownbe1aa822011-07-27 16:04:54 -07002589void TouchInputMapper::configureRawPointerAxes() {
2590 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002591}
2592
Jeff Brownbe1aa822011-07-27 16:04:54 -07002593void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2594 dump.append(INDENT3 "Raw Touch Axes:\n");
2595 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2596 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2597 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2598 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2599 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2600 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2601 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2602 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2603 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002604 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2605 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002606 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2607 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002608}
2609
Jeff Brown65fd2512011-08-18 11:20:58 -07002610void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2611 int32_t oldDeviceMode = mDeviceMode;
2612
2613 // Determine device mode.
2614 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2615 && mConfig.pointerGesturesEnabled) {
2616 mSource = AINPUT_SOURCE_MOUSE;
2617 mDeviceMode = DEVICE_MODE_POINTER;
2618 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2619 && mParameters.associatedDisplayId >= 0) {
2620 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2621 mDeviceMode = DEVICE_MODE_DIRECT;
2622 } else {
2623 mSource = AINPUT_SOURCE_TOUCHPAD;
2624 mDeviceMode = DEVICE_MODE_UNSCALED;
2625 }
2626
Jeff Brown9626b142011-03-03 02:09:54 -08002627 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002628 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002629 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2630 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002631 mDeviceMode = DEVICE_MODE_DISABLED;
2632 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002633 }
2634
Jeff Brown65fd2512011-08-18 11:20:58 -07002635 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002636 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002637 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002638 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002639 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2640 &mAssociatedDisplayOrientation)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002641 LOGI(INDENT "Touch device '%s' could not query the properties of its associated "
2642 "display %d. The device will be inoperable until the display size "
2643 "becomes available.",
2644 getDeviceName().string(), mParameters.associatedDisplayId);
2645 mDeviceMode = DEVICE_MODE_DISABLED;
2646 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002647 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002648 }
2649
Jeff Brown65fd2512011-08-18 11:20:58 -07002650 // Configure dimensions.
2651 int32_t width, height, orientation;
2652 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2653 width = mAssociatedDisplayWidth;
2654 height = mAssociatedDisplayHeight;
2655 orientation = mParameters.orientationAware ?
2656 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2657 } else {
2658 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2659 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2660 orientation = DISPLAY_ORIENTATION_0;
2661 }
2662
2663 // If moving between pointer modes, need to reset some state.
2664 bool deviceModeChanged;
2665 if (mDeviceMode != oldDeviceMode) {
2666 deviceModeChanged = true;
2667
2668 if (mDeviceMode == DEVICE_MODE_POINTER) {
2669 if (mPointerController == NULL) {
2670 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2671 }
2672 } else {
2673 mPointerController.clear();
2674 }
2675
2676 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002677 }
2678
Jeff Brownbe1aa822011-07-27 16:04:54 -07002679 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002680 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002681 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002682 }
2683
Jeff Brownbe1aa822011-07-27 16:04:54 -07002684 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002685 if (sizeChanged || deviceModeChanged) {
2686 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
2687 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002688
Jeff Brownbe1aa822011-07-27 16:04:54 -07002689 mSurfaceWidth = width;
2690 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002691
Jeff Brown8d608662010-08-30 03:02:23 -07002692 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002693 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2694 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2695 mXPrecision = 1.0f / mXScale;
2696 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002697
Jeff Brownbe1aa822011-07-27 16:04:54 -07002698 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002699 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002700 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002701 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002702
Jeff Brownbe1aa822011-07-27 16:04:54 -07002703 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002704
Jeff Brown8d608662010-08-30 03:02:23 -07002705 // Scale factor for terms that are not oriented in a particular axis.
2706 // If the pixels are square then xScale == yScale otherwise we fake it
2707 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002708 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002709
Jeff Brown8d608662010-08-30 03:02:23 -07002710 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002711 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002712
Jeff Browna1f89ce2011-08-11 00:05:01 -07002713 // Size factors.
2714 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2715 if (mRawPointerAxes.touchMajor.valid
2716 && mRawPointerAxes.touchMajor.maxValue != 0) {
2717 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2718 } else if (mRawPointerAxes.toolMajor.valid
2719 && mRawPointerAxes.toolMajor.maxValue != 0) {
2720 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2721 } else {
2722 mSizeScale = 0.0f;
2723 }
2724
Jeff Brownbe1aa822011-07-27 16:04:54 -07002725 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002726 mOrientedRanges.haveToolSize = true;
2727 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002728
Jeff Brownbe1aa822011-07-27 16:04:54 -07002729 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002730 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002731 mOrientedRanges.touchMajor.min = 0;
2732 mOrientedRanges.touchMajor.max = diagonalSize;
2733 mOrientedRanges.touchMajor.flat = 0;
2734 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002735
Jeff Brownbe1aa822011-07-27 16:04:54 -07002736 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2737 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002738
Jeff Brownbe1aa822011-07-27 16:04:54 -07002739 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002740 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002741 mOrientedRanges.toolMajor.min = 0;
2742 mOrientedRanges.toolMajor.max = diagonalSize;
2743 mOrientedRanges.toolMajor.flat = 0;
2744 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002745
Jeff Brownbe1aa822011-07-27 16:04:54 -07002746 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2747 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002748
2749 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002750 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002751 mOrientedRanges.size.min = 0;
2752 mOrientedRanges.size.max = 1.0;
2753 mOrientedRanges.size.flat = 0;
2754 mOrientedRanges.size.fuzz = 0;
2755 } else {
2756 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002757 }
2758
2759 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002760 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002761 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2762 || mCalibration.pressureCalibration
2763 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2764 if (mCalibration.havePressureScale) {
2765 mPressureScale = mCalibration.pressureScale;
2766 } else if (mRawPointerAxes.pressure.valid
2767 && mRawPointerAxes.pressure.maxValue != 0) {
2768 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002769 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002770 }
Jeff Brown8d608662010-08-30 03:02:23 -07002771
Jeff Brown65fd2512011-08-18 11:20:58 -07002772 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2773 mOrientedRanges.pressure.source = mSource;
2774 mOrientedRanges.pressure.min = 0;
2775 mOrientedRanges.pressure.max = 1.0;
2776 mOrientedRanges.pressure.flat = 0;
2777 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002778
Jeff Brown65fd2512011-08-18 11:20:58 -07002779 // Tilt
2780 mTiltXCenter = 0;
2781 mTiltXScale = 0;
2782 mTiltYCenter = 0;
2783 mTiltYScale = 0;
2784 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2785 if (mHaveTilt) {
2786 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2787 mRawPointerAxes.tiltX.maxValue);
2788 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2789 mRawPointerAxes.tiltY.maxValue);
2790 mTiltXScale = M_PI / 180;
2791 mTiltYScale = M_PI / 180;
2792
2793 mOrientedRanges.haveTilt = true;
2794
2795 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2796 mOrientedRanges.tilt.source = mSource;
2797 mOrientedRanges.tilt.min = 0;
2798 mOrientedRanges.tilt.max = M_PI_2;
2799 mOrientedRanges.tilt.flat = 0;
2800 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002801 }
2802
Jeff Brown8d608662010-08-30 03:02:23 -07002803 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002804 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002805 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002806 if (mHaveTilt) {
2807 mOrientedRanges.haveOrientation = true;
2808
2809 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2810 mOrientedRanges.orientation.source = mSource;
2811 mOrientedRanges.orientation.min = -M_PI;
2812 mOrientedRanges.orientation.max = M_PI;
2813 mOrientedRanges.orientation.flat = 0;
2814 mOrientedRanges.orientation.fuzz = 0;
2815 } else if (mCalibration.orientationCalibration !=
2816 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002817 if (mCalibration.orientationCalibration
2818 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002819 if (mRawPointerAxes.orientation.valid) {
2820 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2821 mRawPointerAxes.orientation.maxValue);
2822 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2823 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002824 }
2825 }
2826
Jeff Brownbe1aa822011-07-27 16:04:54 -07002827 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002828
Jeff Brownbe1aa822011-07-27 16:04:54 -07002829 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002830 mOrientedRanges.orientation.source = mSource;
2831 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002832 mOrientedRanges.orientation.max = M_PI_2;
2833 mOrientedRanges.orientation.flat = 0;
2834 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002835 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002836
2837 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002838 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002839 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2840 if (mCalibration.distanceCalibration
2841 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2842 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002843 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002844 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002845 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002846 }
2847 }
2848
Jeff Brownbe1aa822011-07-27 16:04:54 -07002849 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002850
Jeff Brownbe1aa822011-07-27 16:04:54 -07002851 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002852 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002853 mOrientedRanges.distance.min =
2854 mRawPointerAxes.distance.minValue * mDistanceScale;
2855 mOrientedRanges.distance.max =
2856 mRawPointerAxes.distance.minValue * mDistanceScale;
2857 mOrientedRanges.distance.flat = 0;
2858 mOrientedRanges.distance.fuzz =
2859 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002860 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002861 }
2862
Jeff Brown65fd2512011-08-18 11:20:58 -07002863 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002864 // Compute oriented surface dimensions, precision, scales and ranges.
2865 // Note that the maximum value reported is an inclusive maximum value so it is one
2866 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002867 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002868 case DISPLAY_ORIENTATION_90:
2869 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002870 mOrientedSurfaceWidth = mSurfaceHeight;
2871 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002872
Jeff Brownbe1aa822011-07-27 16:04:54 -07002873 mOrientedXPrecision = mYPrecision;
2874 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002875
Jeff Brownbe1aa822011-07-27 16:04:54 -07002876 mOrientedRanges.x.min = 0;
2877 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2878 * mYScale;
2879 mOrientedRanges.x.flat = 0;
2880 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002881
Jeff Brownbe1aa822011-07-27 16:04:54 -07002882 mOrientedRanges.y.min = 0;
2883 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2884 * mXScale;
2885 mOrientedRanges.y.flat = 0;
2886 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002887 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002888
Jeff Brown6d0fec22010-07-23 21:28:06 -07002889 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002890 mOrientedSurfaceWidth = mSurfaceWidth;
2891 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002892
Jeff Brownbe1aa822011-07-27 16:04:54 -07002893 mOrientedXPrecision = mXPrecision;
2894 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002895
Jeff Brownbe1aa822011-07-27 16:04:54 -07002896 mOrientedRanges.x.min = 0;
2897 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2898 * mXScale;
2899 mOrientedRanges.x.flat = 0;
2900 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002901
Jeff Brownbe1aa822011-07-27 16:04:54 -07002902 mOrientedRanges.y.min = 0;
2903 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2904 * mYScale;
2905 mOrientedRanges.y.flat = 0;
2906 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002907 break;
2908 }
Jeff Brownace13b12011-03-09 17:39:48 -08002909
2910 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002911 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002912 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2913 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002914 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002915 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2916 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002917
Jeff Brown2352b972011-04-12 22:39:53 -07002918 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d42011-06-01 12:33:19 -07002919 // given area relative to the diagonal size of the display when no acceleration
2920 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002921 // Assume that the touch pad has a square aspect ratio such that movements in
2922 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07002923 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002924 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002925 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002926
2927 // Scale zooms to cover a smaller range of the display than movements do.
2928 // This value determines the area around the pointer that is affected by freeform
2929 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07002930 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002931 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002932 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002933
Jeff Brown2352b972011-04-12 22:39:53 -07002934 // Max width between pointers to detect a swipe gesture is more than some fraction
2935 // of the diagonal axis of the touch pad. Touches that are wider than this are
2936 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002937 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002938 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002939 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002940
Jeff Brown65fd2512011-08-18 11:20:58 -07002941 // Abort current pointer usages because the state has changed.
2942 abortPointerUsage(when, 0 /*policyFlags*/);
2943
2944 // Inform the dispatcher about the changes.
2945 *outResetNeeded = true;
2946 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002947}
2948
Jeff Brownbe1aa822011-07-27 16:04:54 -07002949void TouchInputMapper::dumpSurface(String8& dump) {
2950 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2951 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2952 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002953}
2954
Jeff Brownbe1aa822011-07-27 16:04:54 -07002955void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002956 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002957 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002958
Jeff Brownbe1aa822011-07-27 16:04:54 -07002959 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002960
Jeff Brown6328cdc2010-07-29 18:18:33 -07002961 if (virtualKeyDefinitions.size() == 0) {
2962 return;
2963 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002964
Jeff Brownbe1aa822011-07-27 16:04:54 -07002965 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002966
Jeff Brownbe1aa822011-07-27 16:04:54 -07002967 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2968 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2969 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2970 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002971
2972 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002973 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002974 virtualKeyDefinitions[i];
2975
Jeff Brownbe1aa822011-07-27 16:04:54 -07002976 mVirtualKeys.add();
2977 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002978
2979 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2980 int32_t keyCode;
2981 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002982 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002983 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002984 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2985 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002986 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07002987 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002988 }
2989
Jeff Brown6328cdc2010-07-29 18:18:33 -07002990 virtualKey.keyCode = keyCode;
2991 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002992
Jeff Brown6328cdc2010-07-29 18:18:33 -07002993 // convert the key definition's display coordinates into touch coordinates for a hit box
2994 int32_t halfWidth = virtualKeyDefinition.width / 2;
2995 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002996
Jeff Brown6328cdc2010-07-29 18:18:33 -07002997 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002998 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002999 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003000 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003001 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003002 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003003 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003004 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003005 }
3006}
3007
Jeff Brownbe1aa822011-07-27 16:04:54 -07003008void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3009 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003010 dump.append(INDENT3 "Virtual Keys:\n");
3011
Jeff Brownbe1aa822011-07-27 16:04:54 -07003012 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3013 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003014 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3015 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3016 i, virtualKey.scanCode, virtualKey.keyCode,
3017 virtualKey.hitLeft, virtualKey.hitRight,
3018 virtualKey.hitTop, virtualKey.hitBottom);
3019 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003020 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003021}
3022
Jeff Brown8d608662010-08-30 03:02:23 -07003023void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003024 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003025 Calibration& out = mCalibration;
3026
Jeff Browna1f89ce2011-08-11 00:05:01 -07003027 // Size
3028 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3029 String8 sizeCalibrationString;
3030 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3031 if (sizeCalibrationString == "none") {
3032 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3033 } else if (sizeCalibrationString == "geometric") {
3034 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3035 } else if (sizeCalibrationString == "diameter") {
3036 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3037 } else if (sizeCalibrationString == "area") {
3038 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3039 } else if (sizeCalibrationString != "default") {
3040 LOGW("Invalid value for touch.size.calibration: '%s'",
3041 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003042 }
3043 }
3044
Jeff Browna1f89ce2011-08-11 00:05:01 -07003045 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3046 out.sizeScale);
3047 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3048 out.sizeBias);
3049 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3050 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003051
3052 // Pressure
3053 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3054 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003055 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003056 if (pressureCalibrationString == "none") {
3057 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3058 } else if (pressureCalibrationString == "physical") {
3059 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3060 } else if (pressureCalibrationString == "amplitude") {
3061 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3062 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003063 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003064 pressureCalibrationString.string());
3065 }
3066 }
3067
Jeff Brown8d608662010-08-30 03:02:23 -07003068 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3069 out.pressureScale);
3070
Jeff Brown8d608662010-08-30 03:02:23 -07003071 // Orientation
3072 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3073 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003074 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003075 if (orientationCalibrationString == "none") {
3076 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3077 } else if (orientationCalibrationString == "interpolated") {
3078 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003079 } else if (orientationCalibrationString == "vector") {
3080 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003081 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003082 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003083 orientationCalibrationString.string());
3084 }
3085 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003086
3087 // Distance
3088 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3089 String8 distanceCalibrationString;
3090 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3091 if (distanceCalibrationString == "none") {
3092 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3093 } else if (distanceCalibrationString == "scaled") {
3094 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3095 } else if (distanceCalibrationString != "default") {
3096 LOGW("Invalid value for touch.distance.calibration: '%s'",
3097 distanceCalibrationString.string());
3098 }
3099 }
3100
3101 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3102 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003103}
3104
3105void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003106 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003107 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3108 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3109 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003110 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003111 } else {
3112 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3113 }
Jeff Brown8d608662010-08-30 03:02:23 -07003114
Jeff Browna1f89ce2011-08-11 00:05:01 -07003115 // Pressure
3116 if (mRawPointerAxes.pressure.valid) {
3117 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3118 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3119 }
3120 } else {
3121 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003122 }
3123
3124 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003125 if (mRawPointerAxes.orientation.valid) {
3126 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003127 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003128 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003129 } else {
3130 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003131 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003132
3133 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003134 if (mRawPointerAxes.distance.valid) {
3135 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003136 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003137 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003138 } else {
3139 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003140 }
Jeff Brown8d608662010-08-30 03:02:23 -07003141}
3142
Jeff Brownef3d7e82010-09-30 14:33:04 -07003143void TouchInputMapper::dumpCalibration(String8& dump) {
3144 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003145
Jeff Browna1f89ce2011-08-11 00:05:01 -07003146 // Size
3147 switch (mCalibration.sizeCalibration) {
3148 case Calibration::SIZE_CALIBRATION_NONE:
3149 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003150 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003151 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3152 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003153 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003154 case Calibration::SIZE_CALIBRATION_DIAMETER:
3155 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3156 break;
3157 case Calibration::SIZE_CALIBRATION_AREA:
3158 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003159 break;
3160 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003161 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003162 }
3163
Jeff Browna1f89ce2011-08-11 00:05:01 -07003164 if (mCalibration.haveSizeScale) {
3165 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3166 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003167 }
3168
Jeff Browna1f89ce2011-08-11 00:05:01 -07003169 if (mCalibration.haveSizeBias) {
3170 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3171 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003172 }
3173
Jeff Browna1f89ce2011-08-11 00:05:01 -07003174 if (mCalibration.haveSizeIsSummed) {
3175 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3176 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003177 }
3178
3179 // Pressure
3180 switch (mCalibration.pressureCalibration) {
3181 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003182 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003183 break;
3184 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003185 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003186 break;
3187 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003188 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003189 break;
3190 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003191 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003192 }
3193
Jeff Brown8d608662010-08-30 03:02:23 -07003194 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003195 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3196 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003197 }
3198
Jeff Brown8d608662010-08-30 03:02:23 -07003199 // Orientation
3200 switch (mCalibration.orientationCalibration) {
3201 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003202 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003203 break;
3204 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003205 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003206 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003207 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3208 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3209 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003210 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003211 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003212 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003213
3214 // Distance
3215 switch (mCalibration.distanceCalibration) {
3216 case Calibration::DISTANCE_CALIBRATION_NONE:
3217 dump.append(INDENT4 "touch.distance.calibration: none\n");
3218 break;
3219 case Calibration::DISTANCE_CALIBRATION_SCALED:
3220 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3221 break;
3222 default:
3223 LOG_ASSERT(false);
3224 }
3225
3226 if (mCalibration.haveDistanceScale) {
3227 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3228 mCalibration.distanceScale);
3229 }
Jeff Brown8d608662010-08-30 03:02:23 -07003230}
3231
Jeff Brown65fd2512011-08-18 11:20:58 -07003232void TouchInputMapper::reset(nsecs_t when) {
3233 mCursorButtonAccumulator.reset(getDevice());
3234 mCursorScrollAccumulator.reset(getDevice());
3235 mTouchButtonAccumulator.reset(getDevice());
3236
3237 mPointerVelocityControl.reset();
3238 mWheelXVelocityControl.reset();
3239 mWheelYVelocityControl.reset();
3240
Jeff Brownbe1aa822011-07-27 16:04:54 -07003241 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003242 mLastRawPointerData.clear();
3243 mCurrentCookedPointerData.clear();
3244 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003245 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003246 mLastButtonState = 0;
3247 mCurrentRawVScroll = 0;
3248 mCurrentRawHScroll = 0;
3249 mCurrentFingerIdBits.clear();
3250 mLastFingerIdBits.clear();
3251 mCurrentStylusIdBits.clear();
3252 mLastStylusIdBits.clear();
3253 mCurrentMouseIdBits.clear();
3254 mLastMouseIdBits.clear();
3255 mPointerUsage = POINTER_USAGE_NONE;
3256 mSentHoverEnter = false;
3257 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003258
Jeff Brown65fd2512011-08-18 11:20:58 -07003259 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003260
Jeff Brown65fd2512011-08-18 11:20:58 -07003261 mPointerGesture.reset();
3262 mPointerSimple.reset();
3263
3264 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003265 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3266 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003267 }
3268
Jeff Brown65fd2512011-08-18 11:20:58 -07003269 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003270}
3271
Jeff Brown65fd2512011-08-18 11:20:58 -07003272void TouchInputMapper::process(const RawEvent* rawEvent) {
3273 mCursorButtonAccumulator.process(rawEvent);
3274 mCursorScrollAccumulator.process(rawEvent);
3275 mTouchButtonAccumulator.process(rawEvent);
3276
3277 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
3278 sync(rawEvent->when);
3279 }
3280}
3281
3282void TouchInputMapper::sync(nsecs_t when) {
3283 // Sync button state.
3284 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3285 | mCursorButtonAccumulator.getButtonState();
3286
3287 // Sync scroll state.
3288 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3289 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3290 mCursorScrollAccumulator.finishSync();
3291
3292 // Sync touch state.
3293 bool havePointerIds = true;
3294 mCurrentRawPointerData.clear();
3295 syncTouch(when, &havePointerIds);
3296
Jeff Brownaa3855d2011-03-17 01:34:19 -07003297#if DEBUG_RAW_EVENTS
3298 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003299 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3300 mLastRawPointerData.pointerCount,
3301 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003302 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003303 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3304 "hovering ids 0x%08x -> 0x%08x",
3305 mLastRawPointerData.pointerCount,
3306 mCurrentRawPointerData.pointerCount,
3307 mLastRawPointerData.touchingIdBits.value,
3308 mCurrentRawPointerData.touchingIdBits.value,
3309 mLastRawPointerData.hoveringIdBits.value,
3310 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003311 }
3312#endif
3313
Jeff Brown65fd2512011-08-18 11:20:58 -07003314 // Reset state that we will compute below.
3315 mCurrentFingerIdBits.clear();
3316 mCurrentStylusIdBits.clear();
3317 mCurrentMouseIdBits.clear();
3318 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003319
Jeff Brown65fd2512011-08-18 11:20:58 -07003320 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3321 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003322 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003323 mCurrentButtonState = 0;
3324 } else {
3325 // Preprocess pointer data.
3326 if (!havePointerIds) {
3327 assignPointerIds();
3328 }
3329
3330 // Handle policy on initial down or hover events.
3331 uint32_t policyFlags = 0;
3332 if (mLastRawPointerData.pointerCount == 0 && mCurrentRawPointerData.pointerCount != 0) {
3333 if (mDeviceMode == DEVICE_MODE_DIRECT) {
3334 // If this is a touch screen, hide the pointer on an initial down.
3335 getContext()->fadePointer();
3336 }
3337
3338 // Initial downs on external touch devices should wake the device.
3339 // We don't do this for internal touch screens to prevent them from waking
3340 // up in your pocket.
3341 // TODO: Use the input device configuration to control this behavior more finely.
3342 if (getDevice()->isExternal()) {
3343 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3344 }
3345 }
3346
3347 // Synthesize key down from raw buttons if needed.
3348 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3349 policyFlags, mLastButtonState, mCurrentButtonState);
3350
3351 // Consume raw off-screen touches before cooking pointer data.
3352 // If touches are consumed, subsequent code will not receive any pointer data.
3353 if (consumeRawTouches(when, policyFlags)) {
3354 mCurrentRawPointerData.clear();
3355 }
3356
3357 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3358 // with cooked pointer data that has the same ids and indices as the raw data.
3359 // The following code can use either the raw or cooked data, as needed.
3360 cookPointerData();
3361
3362 // Dispatch the touches either directly or by translation through a pointer on screen.
3363 if (mPointerController != NULL) {
3364 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3365 uint32_t id = idBits.clearFirstMarkedBit();
3366 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3367 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3368 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3369 mCurrentStylusIdBits.markBit(id);
3370 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3371 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3372 mCurrentFingerIdBits.markBit(id);
3373 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3374 mCurrentMouseIdBits.markBit(id);
3375 }
3376 }
3377 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3378 uint32_t id = idBits.clearFirstMarkedBit();
3379 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3380 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3381 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3382 mCurrentStylusIdBits.markBit(id);
3383 }
3384 }
3385
3386 // Stylus takes precedence over all tools, then mouse, then finger.
3387 PointerUsage pointerUsage = mPointerUsage;
3388 if (!mCurrentStylusIdBits.isEmpty()) {
3389 mCurrentMouseIdBits.clear();
3390 mCurrentFingerIdBits.clear();
3391 pointerUsage = POINTER_USAGE_STYLUS;
3392 } else if (!mCurrentMouseIdBits.isEmpty()) {
3393 mCurrentFingerIdBits.clear();
3394 pointerUsage = POINTER_USAGE_MOUSE;
3395 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3396 pointerUsage = POINTER_USAGE_GESTURES;
3397 } else {
3398 pointerUsage = POINTER_USAGE_NONE;
3399 }
3400
3401 dispatchPointerUsage(when, policyFlags, pointerUsage);
3402 } else {
3403 dispatchHoverExit(when, policyFlags);
3404 dispatchTouches(when, policyFlags);
3405 dispatchHoverEnterAndMove(when, policyFlags);
3406 }
3407
3408 // Synthesize key up from raw buttons if needed.
3409 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3410 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003411 }
3412
Jeff Brown6328cdc2010-07-29 18:18:33 -07003413 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003414 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3415 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3416 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003417 mLastFingerIdBits = mCurrentFingerIdBits;
3418 mLastStylusIdBits = mCurrentStylusIdBits;
3419 mLastMouseIdBits = mCurrentMouseIdBits;
3420
3421 // Clear some transient state.
3422 mCurrentRawVScroll = 0;
3423 mCurrentRawHScroll = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003424}
3425
Jeff Brown79ac9692011-04-19 21:20:10 -07003426void TouchInputMapper::timeoutExpired(nsecs_t when) {
3427 if (mPointerController != NULL) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003428 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3429 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3430 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003431 }
3432}
3433
Jeff Brownbe1aa822011-07-27 16:04:54 -07003434bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3435 // Check for release of a virtual key.
3436 if (mCurrentVirtualKey.down) {
3437 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3438 // Pointer went up while virtual key was down.
3439 mCurrentVirtualKey.down = false;
3440 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003441#if DEBUG_VIRTUAL_KEYS
3442 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003443 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003444#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003445 dispatchVirtualKey(when, policyFlags,
3446 AKEY_EVENT_ACTION_UP,
3447 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003448 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003449 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003450 }
3451
Jeff Brownbe1aa822011-07-27 16:04:54 -07003452 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3453 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3454 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3455 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3456 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3457 // Pointer is still within the space of the virtual key.
3458 return true;
3459 }
3460 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003461
Jeff Brownbe1aa822011-07-27 16:04:54 -07003462 // Pointer left virtual key area or another pointer also went down.
3463 // Send key cancellation but do not consume the touch yet.
3464 // This is useful when the user swipes through from the virtual key area
3465 // into the main display surface.
3466 mCurrentVirtualKey.down = false;
3467 if (!mCurrentVirtualKey.ignored) {
3468#if DEBUG_VIRTUAL_KEYS
3469 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3470 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3471#endif
3472 dispatchVirtualKey(when, policyFlags,
3473 AKEY_EVENT_ACTION_UP,
3474 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3475 | AKEY_EVENT_FLAG_CANCELED);
3476 }
3477 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003478
Jeff Brownbe1aa822011-07-27 16:04:54 -07003479 if (mLastRawPointerData.touchingIdBits.isEmpty()
3480 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3481 // Pointer just went down. Check for virtual key press or off-screen touches.
3482 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3483 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3484 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3485 // If exactly one pointer went down, check for virtual key hit.
3486 // Otherwise we will drop the entire stroke.
3487 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3488 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3489 if (virtualKey) {
3490 mCurrentVirtualKey.down = true;
3491 mCurrentVirtualKey.downTime = when;
3492 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3493 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3494 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3495 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3496
3497 if (!mCurrentVirtualKey.ignored) {
3498#if DEBUG_VIRTUAL_KEYS
3499 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3500 mCurrentVirtualKey.keyCode,
3501 mCurrentVirtualKey.scanCode);
3502#endif
3503 dispatchVirtualKey(when, policyFlags,
3504 AKEY_EVENT_ACTION_DOWN,
3505 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3506 }
3507 }
3508 }
3509 return true;
3510 }
3511 }
3512
Jeff Brownfe508922011-01-18 15:10:10 -08003513 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003514 // most recent touch within the screen area. The idea is to filter out stray
3515 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003516 //
3517 // Problems we're trying to solve:
3518 //
3519 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3520 // virtual key area that is implemented by a separate touch panel and accidentally
3521 // triggers a virtual key.
3522 //
3523 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3524 // area and accidentally triggers a virtual key. This often happens when virtual keys
3525 // are layed out below the screen near to where the on screen keyboard's space bar
3526 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003527 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003528 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003529 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003530 return false;
3531}
3532
3533void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3534 int32_t keyEventAction, int32_t keyEventFlags) {
3535 int32_t keyCode = mCurrentVirtualKey.keyCode;
3536 int32_t scanCode = mCurrentVirtualKey.scanCode;
3537 nsecs_t downTime = mCurrentVirtualKey.downTime;
3538 int32_t metaState = mContext->getGlobalMetaState();
3539 policyFlags |= POLICY_FLAG_VIRTUAL;
3540
3541 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3542 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3543 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003544}
3545
Jeff Brown6d0fec22010-07-23 21:28:06 -07003546void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003547 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3548 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003549 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003550 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003551
3552 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003553 if (!currentIdBits.isEmpty()) {
3554 // No pointer id changes so this is a move event.
3555 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003556 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003557 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3558 AMOTION_EVENT_EDGE_FLAG_NONE,
3559 mCurrentCookedPointerData.pointerProperties,
3560 mCurrentCookedPointerData.pointerCoords,
3561 mCurrentCookedPointerData.idToIndex,
3562 currentIdBits, -1,
3563 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3564 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003565 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003566 // There may be pointers going up and pointers going down and pointers moving
3567 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003568 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3569 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003570 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003571 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003572
Jeff Brownace13b12011-03-09 17:39:48 -08003573 // Update last coordinates of pointers that have moved so that we observe the new
3574 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003575 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003576 mCurrentCookedPointerData.pointerProperties,
3577 mCurrentCookedPointerData.pointerCoords,
3578 mCurrentCookedPointerData.idToIndex,
3579 mLastCookedPointerData.pointerProperties,
3580 mLastCookedPointerData.pointerCoords,
3581 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003582 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003583 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003584 moveNeeded = true;
3585 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003586
Jeff Brownace13b12011-03-09 17:39:48 -08003587 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003588 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003589 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003590
Jeff Brown65fd2512011-08-18 11:20:58 -07003591 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003592 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003593 mLastCookedPointerData.pointerProperties,
3594 mLastCookedPointerData.pointerCoords,
3595 mLastCookedPointerData.idToIndex,
3596 dispatchedIdBits, upId,
3597 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003598 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003599 }
3600
Jeff Brownc3db8582010-10-20 15:33:38 -07003601 // Dispatch move events if any of the remaining pointers moved from their old locations.
3602 // Although applications receive new locations as part of individual pointer up
3603 // events, they do not generally handle them except when presented in a move event.
3604 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003605 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003606 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003607 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003608 mCurrentCookedPointerData.pointerProperties,
3609 mCurrentCookedPointerData.pointerCoords,
3610 mCurrentCookedPointerData.idToIndex,
3611 dispatchedIdBits, -1,
3612 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003613 }
3614
3615 // Dispatch pointer down events using the new pointer locations.
3616 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003617 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003618 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003619
Jeff Brownace13b12011-03-09 17:39:48 -08003620 if (dispatchedIdBits.count() == 1) {
3621 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003622 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003623 }
3624
Jeff Brown65fd2512011-08-18 11:20:58 -07003625 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003626 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003627 mCurrentCookedPointerData.pointerProperties,
3628 mCurrentCookedPointerData.pointerCoords,
3629 mCurrentCookedPointerData.idToIndex,
3630 dispatchedIdBits, downId,
3631 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003632 }
3633 }
Jeff Brownace13b12011-03-09 17:39:48 -08003634}
3635
Jeff Brownbe1aa822011-07-27 16:04:54 -07003636void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3637 if (mSentHoverEnter &&
3638 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3639 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3640 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003641 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003642 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3643 mLastCookedPointerData.pointerProperties,
3644 mLastCookedPointerData.pointerCoords,
3645 mLastCookedPointerData.idToIndex,
3646 mLastCookedPointerData.hoveringIdBits, -1,
3647 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3648 mSentHoverEnter = false;
3649 }
3650}
Jeff Brownace13b12011-03-09 17:39:48 -08003651
Jeff Brownbe1aa822011-07-27 16:04:54 -07003652void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3653 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3654 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3655 int32_t metaState = getContext()->getGlobalMetaState();
3656 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003657 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003658 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3659 mCurrentCookedPointerData.pointerProperties,
3660 mCurrentCookedPointerData.pointerCoords,
3661 mCurrentCookedPointerData.idToIndex,
3662 mCurrentCookedPointerData.hoveringIdBits, -1,
3663 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3664 mSentHoverEnter = true;
3665 }
Jeff Brownace13b12011-03-09 17:39:48 -08003666
Jeff Brown65fd2512011-08-18 11:20:58 -07003667 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003668 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3669 mCurrentCookedPointerData.pointerProperties,
3670 mCurrentCookedPointerData.pointerCoords,
3671 mCurrentCookedPointerData.idToIndex,
3672 mCurrentCookedPointerData.hoveringIdBits, -1,
3673 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3674 }
3675}
3676
3677void TouchInputMapper::cookPointerData() {
3678 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3679
3680 mCurrentCookedPointerData.clear();
3681 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3682 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3683 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3684
3685 // Walk through the the active pointers and map device coordinates onto
3686 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003687 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003688 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003689
Jeff Browna1f89ce2011-08-11 00:05:01 -07003690 // Size
3691 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3692 switch (mCalibration.sizeCalibration) {
3693 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3694 case Calibration::SIZE_CALIBRATION_DIAMETER:
3695 case Calibration::SIZE_CALIBRATION_AREA:
3696 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3697 touchMajor = in.touchMajor;
3698 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3699 toolMajor = in.toolMajor;
3700 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3701 size = mRawPointerAxes.touchMinor.valid
3702 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3703 } else if (mRawPointerAxes.touchMajor.valid) {
3704 toolMajor = touchMajor = in.touchMajor;
3705 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3706 ? in.touchMinor : in.touchMajor;
3707 size = mRawPointerAxes.touchMinor.valid
3708 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3709 } else if (mRawPointerAxes.toolMajor.valid) {
3710 touchMajor = toolMajor = in.toolMajor;
3711 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3712 ? in.toolMinor : in.toolMajor;
3713 size = mRawPointerAxes.toolMinor.valid
3714 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003715 } else {
Jeff Browna1f89ce2011-08-11 00:05:01 -07003716 LOG_ASSERT(false, "No touch or tool axes. "
3717 "Size calibration should have been resolved to NONE.");
3718 touchMajor = 0;
3719 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003720 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003721 toolMinor = 0;
3722 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003723 }
Jeff Brownace13b12011-03-09 17:39:48 -08003724
Jeff Browna1f89ce2011-08-11 00:05:01 -07003725 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3726 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3727 if (touchingCount > 1) {
3728 touchMajor /= touchingCount;
3729 touchMinor /= touchingCount;
3730 toolMajor /= touchingCount;
3731 toolMinor /= touchingCount;
3732 size /= touchingCount;
3733 }
3734 }
Jeff Brownace13b12011-03-09 17:39:48 -08003735
Jeff Browna1f89ce2011-08-11 00:05:01 -07003736 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3737 touchMajor *= mGeometricScale;
3738 touchMinor *= mGeometricScale;
3739 toolMajor *= mGeometricScale;
3740 toolMinor *= mGeometricScale;
3741 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3742 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003743 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003744 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3745 toolMinor = toolMajor;
3746 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3747 touchMinor = touchMajor;
3748 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003749 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003750
3751 mCalibration.applySizeScaleAndBias(&touchMajor);
3752 mCalibration.applySizeScaleAndBias(&touchMinor);
3753 mCalibration.applySizeScaleAndBias(&toolMajor);
3754 mCalibration.applySizeScaleAndBias(&toolMinor);
3755 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003756 break;
3757 default:
3758 touchMajor = 0;
3759 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003760 toolMajor = 0;
3761 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003762 size = 0;
3763 break;
3764 }
3765
Jeff Browna1f89ce2011-08-11 00:05:01 -07003766 // Pressure
3767 float pressure;
3768 switch (mCalibration.pressureCalibration) {
3769 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3770 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3771 pressure = in.pressure * mPressureScale;
3772 break;
3773 default:
3774 pressure = in.isHovering ? 0 : 1;
3775 break;
3776 }
3777
Jeff Brown65fd2512011-08-18 11:20:58 -07003778 // Tilt and Orientation
3779 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003780 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003781 if (mHaveTilt) {
3782 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3783 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3784 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3785 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3786 } else {
3787 tilt = 0;
3788
3789 switch (mCalibration.orientationCalibration) {
3790 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3791 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3792 break;
3793 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3794 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3795 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3796 if (c1 != 0 || c2 != 0) {
3797 orientation = atan2f(c1, c2) * 0.5f;
3798 float confidence = hypotf(c1, c2);
3799 float scale = 1.0f + confidence / 16.0f;
3800 touchMajor *= scale;
3801 touchMinor /= scale;
3802 toolMajor *= scale;
3803 toolMinor /= scale;
3804 } else {
3805 orientation = 0;
3806 }
3807 break;
3808 }
3809 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003810 orientation = 0;
3811 }
Jeff Brownace13b12011-03-09 17:39:48 -08003812 }
3813
Jeff Brown80fd47c2011-05-24 01:07:44 -07003814 // Distance
3815 float distance;
3816 switch (mCalibration.distanceCalibration) {
3817 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003818 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003819 break;
3820 default:
3821 distance = 0;
3822 }
3823
Jeff Brownace13b12011-03-09 17:39:48 -08003824 // X and Y
3825 // Adjust coords for surface orientation.
3826 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003827 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003828 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003829 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3830 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003831 orientation -= M_PI_2;
3832 if (orientation < - M_PI_2) {
3833 orientation += M_PI;
3834 }
3835 break;
3836 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003837 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3838 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003839 break;
3840 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003841 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3842 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003843 orientation += M_PI_2;
3844 if (orientation > M_PI_2) {
3845 orientation -= M_PI;
3846 }
3847 break;
3848 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003849 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3850 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003851 break;
3852 }
3853
3854 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003855 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003856 out.clear();
3857 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3858 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3859 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3860 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3861 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3862 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3863 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3864 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3865 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003866 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003867 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003868
3869 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003870 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3871 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003872 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003873 properties.id = id;
3874 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003875
Jeff Brownbe1aa822011-07-27 16:04:54 -07003876 // Write id index.
3877 mCurrentCookedPointerData.idToIndex[id] = i;
3878 }
Jeff Brownace13b12011-03-09 17:39:48 -08003879}
3880
Jeff Brown65fd2512011-08-18 11:20:58 -07003881void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3882 PointerUsage pointerUsage) {
3883 if (pointerUsage != mPointerUsage) {
3884 abortPointerUsage(when, policyFlags);
3885 mPointerUsage = pointerUsage;
3886 }
3887
3888 switch (mPointerUsage) {
3889 case POINTER_USAGE_GESTURES:
3890 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3891 break;
3892 case POINTER_USAGE_STYLUS:
3893 dispatchPointerStylus(when, policyFlags);
3894 break;
3895 case POINTER_USAGE_MOUSE:
3896 dispatchPointerMouse(when, policyFlags);
3897 break;
3898 default:
3899 break;
3900 }
3901}
3902
3903void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3904 switch (mPointerUsage) {
3905 case POINTER_USAGE_GESTURES:
3906 abortPointerGestures(when, policyFlags);
3907 break;
3908 case POINTER_USAGE_STYLUS:
3909 abortPointerStylus(when, policyFlags);
3910 break;
3911 case POINTER_USAGE_MOUSE:
3912 abortPointerMouse(when, policyFlags);
3913 break;
3914 default:
3915 break;
3916 }
3917
3918 mPointerUsage = POINTER_USAGE_NONE;
3919}
3920
Jeff Brown79ac9692011-04-19 21:20:10 -07003921void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3922 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003923 // Update current gesture coordinates.
3924 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003925 bool sendEvents = preparePointerGestures(when,
3926 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3927 if (!sendEvents) {
3928 return;
3929 }
Jeff Brown19c97d42011-06-01 12:33:19 -07003930 if (finishPreviousGesture) {
3931 cancelPreviousGesture = false;
3932 }
Jeff Brownace13b12011-03-09 17:39:48 -08003933
Jeff Brownbb3fcba2011-06-06 19:23:05 -07003934 // Update the pointer presentation and spots.
3935 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3936 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3937 if (finishPreviousGesture || cancelPreviousGesture) {
3938 mPointerController->clearSpots();
3939 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003940 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3941 mPointerGesture.currentGestureIdToIndex,
3942 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07003943 } else {
3944 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3945 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003946
Jeff Brown538881e2011-05-25 18:23:38 -07003947 // Show or hide the pointer if needed.
3948 switch (mPointerGesture.currentGestureMode) {
3949 case PointerGesture::NEUTRAL:
3950 case PointerGesture::QUIET:
3951 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3952 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3953 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3954 // Remind the user of where the pointer is after finishing a gesture with spots.
3955 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3956 }
3957 break;
3958 case PointerGesture::TAP:
3959 case PointerGesture::TAP_DRAG:
3960 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3961 case PointerGesture::HOVER:
3962 case PointerGesture::PRESS:
3963 // Unfade the pointer when the current gesture manipulates the
3964 // area directly under the pointer.
3965 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3966 break;
3967 case PointerGesture::SWIPE:
3968 case PointerGesture::FREEFORM:
3969 // Fade the pointer when the current gesture manipulates a different
3970 // area and there are spots to guide the user experience.
3971 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3972 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3973 } else {
3974 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3975 }
3976 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003977 }
3978
Jeff Brownace13b12011-03-09 17:39:48 -08003979 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003980 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003981 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003982
3983 // Update last coordinates of pointers that have moved so that we observe the new
3984 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003985 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3986 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3987 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003988 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003989 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3990 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3991 bool moveNeeded = false;
3992 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003993 && !mPointerGesture.lastGestureIdBits.isEmpty()
3994 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003995 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3996 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003997 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003998 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003999 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004000 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4001 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004002 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004003 moveNeeded = true;
4004 }
Jeff Brownace13b12011-03-09 17:39:48 -08004005 }
4006
4007 // Send motion events for all pointers that went up or were canceled.
4008 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4009 if (!dispatchedGestureIdBits.isEmpty()) {
4010 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004011 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004012 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4013 AMOTION_EVENT_EDGE_FLAG_NONE,
4014 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004015 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4016 dispatchedGestureIdBits, -1,
4017 0, 0, mPointerGesture.downTime);
4018
4019 dispatchedGestureIdBits.clear();
4020 } else {
4021 BitSet32 upGestureIdBits;
4022 if (finishPreviousGesture) {
4023 upGestureIdBits = dispatchedGestureIdBits;
4024 } else {
4025 upGestureIdBits.value = dispatchedGestureIdBits.value
4026 & ~mPointerGesture.currentGestureIdBits.value;
4027 }
4028 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004029 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004030
Jeff Brown65fd2512011-08-18 11:20:58 -07004031 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004032 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004033 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4034 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004035 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4036 dispatchedGestureIdBits, id,
4037 0, 0, mPointerGesture.downTime);
4038
4039 dispatchedGestureIdBits.clearBit(id);
4040 }
4041 }
4042 }
4043
4044 // Send motion events for all pointers that moved.
4045 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004046 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004047 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4048 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004049 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4050 dispatchedGestureIdBits, -1,
4051 0, 0, mPointerGesture.downTime);
4052 }
4053
4054 // Send motion events for all pointers that went down.
4055 if (down) {
4056 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4057 & ~dispatchedGestureIdBits.value);
4058 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004059 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004060 dispatchedGestureIdBits.markBit(id);
4061
Jeff Brownace13b12011-03-09 17:39:48 -08004062 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004063 mPointerGesture.downTime = when;
4064 }
4065
Jeff Brown65fd2512011-08-18 11:20:58 -07004066 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004067 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004068 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004069 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4070 dispatchedGestureIdBits, id,
4071 0, 0, mPointerGesture.downTime);
4072 }
4073 }
4074
Jeff Brownace13b12011-03-09 17:39:48 -08004075 // Send motion events for hover.
4076 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004077 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004078 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4079 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4080 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004081 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4082 mPointerGesture.currentGestureIdBits, -1,
4083 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004084 } else if (dispatchedGestureIdBits.isEmpty()
4085 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4086 // Synthesize a hover move event after all pointers go up to indicate that
4087 // the pointer is hovering again even if the user is not currently touching
4088 // the touch pad. This ensures that a view will receive a fresh hover enter
4089 // event after a tap.
4090 float x, y;
4091 mPointerController->getPosition(&x, &y);
4092
4093 PointerProperties pointerProperties;
4094 pointerProperties.clear();
4095 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004096 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004097
4098 PointerCoords pointerCoords;
4099 pointerCoords.clear();
4100 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4101 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4102
Jeff Brown65fd2512011-08-18 11:20:58 -07004103 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004104 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4105 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4106 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004107 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004108 }
4109
4110 // Update state.
4111 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4112 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004113 mPointerGesture.lastGestureIdBits.clear();
4114 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004115 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4116 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004117 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004118 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004119 mPointerGesture.lastGestureProperties[index].copyFrom(
4120 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004121 mPointerGesture.lastGestureCoords[index].copyFrom(
4122 mPointerGesture.currentGestureCoords[index]);
4123 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004124 }
4125 }
4126}
4127
Jeff Brown65fd2512011-08-18 11:20:58 -07004128void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4129 // Cancel previously dispatches pointers.
4130 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4131 int32_t metaState = getContext()->getGlobalMetaState();
4132 int32_t buttonState = mCurrentButtonState;
4133 dispatchMotion(when, policyFlags, mSource,
4134 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4135 AMOTION_EVENT_EDGE_FLAG_NONE,
4136 mPointerGesture.lastGestureProperties,
4137 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4138 mPointerGesture.lastGestureIdBits, -1,
4139 0, 0, mPointerGesture.downTime);
4140 }
4141
4142 // Reset the current pointer gesture.
4143 mPointerGesture.reset();
4144 mPointerVelocityControl.reset();
4145
4146 // Remove any current spots.
4147 if (mPointerController != NULL) {
4148 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4149 mPointerController->clearSpots();
4150 }
4151}
4152
Jeff Brown79ac9692011-04-19 21:20:10 -07004153bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4154 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004155 *outCancelPreviousGesture = false;
4156 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004157
Jeff Brown79ac9692011-04-19 21:20:10 -07004158 // Handle TAP timeout.
4159 if (isTimeout) {
4160#if DEBUG_GESTURES
4161 LOGD("Gestures: Processing timeout");
4162#endif
4163
4164 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004165 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004166 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004167 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004168 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004169 } else {
4170 // The tap is finished.
4171#if DEBUG_GESTURES
4172 LOGD("Gestures: TAP finished");
4173#endif
4174 *outFinishPreviousGesture = true;
4175
4176 mPointerGesture.activeGestureId = -1;
4177 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4178 mPointerGesture.currentGestureIdBits.clear();
4179
Jeff Brown65fd2512011-08-18 11:20:58 -07004180 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004181 return true;
4182 }
4183 }
4184
4185 // We did not handle this timeout.
4186 return false;
4187 }
4188
Jeff Brown65fd2512011-08-18 11:20:58 -07004189 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4190 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4191
Jeff Brownace13b12011-03-09 17:39:48 -08004192 // Update the velocity tracker.
4193 {
4194 VelocityTracker::Position positions[MAX_POINTERS];
4195 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004196 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004197 uint32_t id = idBits.clearFirstMarkedBit();
4198 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004199 positions[count].x = pointer.x * mPointerXMovementScale;
4200 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004201 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004202 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004203 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004204 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004205
Jeff Brownace13b12011-03-09 17:39:48 -08004206 // Pick a new active touch id if needed.
4207 // Choose an arbitrary pointer that just went down, if there is one.
4208 // Otherwise choose an arbitrary remaining pointer.
4209 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004210 // We keep the same active touch id for as long as possible.
4211 bool activeTouchChanged = false;
4212 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4213 int32_t activeTouchId = lastActiveTouchId;
4214 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004215 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004216 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004217 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004218 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004219 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004220 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004221 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004222 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004223 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004224 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004225 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004226 } else {
4227 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004228 }
4229 }
4230
4231 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004232 bool isQuietTime = false;
4233 if (activeTouchId < 0) {
4234 mPointerGesture.resetQuietTime();
4235 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004236 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004237 if (!isQuietTime) {
4238 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4239 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4240 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004241 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004242 // Enter quiet time when exiting swipe or freeform state.
4243 // This is to prevent accidentally entering the hover state and flinging the
4244 // pointer when finishing a swipe and there is still one pointer left onscreen.
4245 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004246 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004247 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004248 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004249 // Enter quiet time when releasing the button and there are still two or more
4250 // fingers down. This may indicate that one finger was used to press the button
4251 // but it has not gone up yet.
4252 isQuietTime = true;
4253 }
4254 if (isQuietTime) {
4255 mPointerGesture.quietTime = when;
4256 }
Jeff Brownace13b12011-03-09 17:39:48 -08004257 }
4258 }
4259
4260 // Switch states based on button and pointer state.
4261 if (isQuietTime) {
4262 // Case 1: Quiet time. (QUIET)
4263#if DEBUG_GESTURES
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004264 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004265 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004266#endif
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004267 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4268 *outFinishPreviousGesture = true;
4269 }
Jeff Brownace13b12011-03-09 17:39:48 -08004270
4271 mPointerGesture.activeGestureId = -1;
4272 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004273 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004274
Jeff Brown65fd2512011-08-18 11:20:58 -07004275 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004276 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004277 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004278 // The pointer follows the active touch point.
4279 // Emit DOWN, MOVE, UP events at the pointer location.
4280 //
4281 // Only the active touch matters; other fingers are ignored. This policy helps
4282 // to handle the case where the user places a second finger on the touch pad
4283 // to apply the necessary force to depress an integrated button below the surface.
4284 // We don't want the second finger to be delivered to applications.
4285 //
4286 // For this to work well, we need to make sure to track the pointer that is really
4287 // active. If the user first puts one finger down to click then adds another
4288 // finger to drag then the active pointer should switch to the finger that is
4289 // being dragged.
4290#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004291 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004292 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004293#endif
4294 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004295 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004296 *outFinishPreviousGesture = true;
4297 mPointerGesture.activeGestureId = 0;
4298 }
4299
4300 // Switch pointers if needed.
4301 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004302 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d42011-06-01 12:33:19 -07004303 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004304 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004305 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004306 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d42011-06-01 12:33:19 -07004307 float vx, vy;
4308 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4309 float speed = hypotf(vx, vy);
4310 if (speed > bestSpeed) {
4311 bestId = id;
4312 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004313 }
Jeff Brown8d608662010-08-30 03:02:23 -07004314 }
Jeff Brown19c97d42011-06-01 12:33:19 -07004315 }
4316 if (bestId >= 0 && bestId != activeTouchId) {
4317 mPointerGesture.activeTouchId = activeTouchId = bestId;
4318 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004319#if DEBUG_GESTURES
Jeff Brown19c97d42011-06-01 12:33:19 -07004320 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4321 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004322#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004323 }
Jeff Brown19c97d42011-06-01 12:33:19 -07004324 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004325
Jeff Brown65fd2512011-08-18 11:20:58 -07004326 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004327 const RawPointerData::Pointer& currentPointer =
4328 mCurrentRawPointerData.pointerForId(activeTouchId);
4329 const RawPointerData::Pointer& lastPointer =
4330 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004331 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4332 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004333
Jeff Brownbe1aa822011-07-27 16:04:54 -07004334 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004335 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d42011-06-01 12:33:19 -07004336
4337 // Move the pointer using a relative motion.
4338 // When using spots, the click will occur at the position of the anchor
4339 // spot and all other spots will move there.
4340 mPointerController->move(deltaX, deltaY);
4341 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004342 mPointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004343 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004344
Jeff Brownace13b12011-03-09 17:39:48 -08004345 float x, y;
4346 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004347
Jeff Brown79ac9692011-04-19 21:20:10 -07004348 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004349 mPointerGesture.currentGestureIdBits.clear();
4350 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4351 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004352 mPointerGesture.currentGestureProperties[0].clear();
4353 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004354 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004355 mPointerGesture.currentGestureCoords[0].clear();
4356 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4357 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4358 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004359 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004360 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004361 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4362 *outFinishPreviousGesture = true;
4363 }
Jeff Brownace13b12011-03-09 17:39:48 -08004364
Jeff Brown79ac9692011-04-19 21:20:10 -07004365 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004366 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004367 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004368 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4369 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004370 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004371 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004372 float x, y;
4373 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004374 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4375 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004376#if DEBUG_GESTURES
4377 LOGD("Gestures: TAP");
4378#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004379
4380 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004381 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004382 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004383
Jeff Brownace13b12011-03-09 17:39:48 -08004384 mPointerGesture.activeGestureId = 0;
4385 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004386 mPointerGesture.currentGestureIdBits.clear();
4387 mPointerGesture.currentGestureIdBits.markBit(
4388 mPointerGesture.activeGestureId);
4389 mPointerGesture.currentGestureIdToIndex[
4390 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004391 mPointerGesture.currentGestureProperties[0].clear();
4392 mPointerGesture.currentGestureProperties[0].id =
4393 mPointerGesture.activeGestureId;
4394 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004395 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004396 mPointerGesture.currentGestureCoords[0].clear();
4397 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004398 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004399 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004400 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004401 mPointerGesture.currentGestureCoords[0].setAxisValue(
4402 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004403
Jeff Brownace13b12011-03-09 17:39:48 -08004404 tapped = true;
4405 } else {
4406#if DEBUG_GESTURES
4407 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004408 x - mPointerGesture.tapX,
4409 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004410#endif
4411 }
4412 } else {
4413#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004414 LOGD("Gestures: Not a TAP, %0.3fms since down",
4415 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004416#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004417 }
Jeff Brownace13b12011-03-09 17:39:48 -08004418 }
Jeff Brown2352b972011-04-12 22:39:53 -07004419
Jeff Brown65fd2512011-08-18 11:20:58 -07004420 mPointerVelocityControl.reset();
Jeff Brown19c97d42011-06-01 12:33:19 -07004421
Jeff Brownace13b12011-03-09 17:39:48 -08004422 if (!tapped) {
4423#if DEBUG_GESTURES
4424 LOGD("Gestures: NEUTRAL");
4425#endif
4426 mPointerGesture.activeGestureId = -1;
4427 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004428 mPointerGesture.currentGestureIdBits.clear();
4429 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004430 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004431 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004432 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004433 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4434 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004435 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004436
Jeff Brown79ac9692011-04-19 21:20:10 -07004437 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4438 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004439 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004440 float x, y;
4441 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004442 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4443 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004444 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4445 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004446#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004447 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4448 x - mPointerGesture.tapX,
4449 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004450#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004451 }
4452 } else {
4453#if DEBUG_GESTURES
4454 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4455 (when - mPointerGesture.tapUpTime) * 0.000001f);
4456#endif
4457 }
4458 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4459 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4460 }
Jeff Brownace13b12011-03-09 17:39:48 -08004461
Jeff Brown65fd2512011-08-18 11:20:58 -07004462 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004463 const RawPointerData::Pointer& currentPointer =
4464 mCurrentRawPointerData.pointerForId(activeTouchId);
4465 const RawPointerData::Pointer& lastPointer =
4466 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004467 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004468 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004469 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004470 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004471
Jeff Brownbe1aa822011-07-27 16:04:54 -07004472 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004473 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d42011-06-01 12:33:19 -07004474
Jeff Brown2352b972011-04-12 22:39:53 -07004475 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004476 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004477 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d42011-06-01 12:33:19 -07004478 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004479 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004480 }
4481
Jeff Brown79ac9692011-04-19 21:20:10 -07004482 bool down;
4483 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4484#if DEBUG_GESTURES
4485 LOGD("Gestures: TAP_DRAG");
4486#endif
4487 down = true;
4488 } else {
4489#if DEBUG_GESTURES
4490 LOGD("Gestures: HOVER");
4491#endif
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004492 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4493 *outFinishPreviousGesture = true;
4494 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004495 mPointerGesture.activeGestureId = 0;
4496 down = false;
4497 }
Jeff Brownace13b12011-03-09 17:39:48 -08004498
4499 float x, y;
4500 mPointerController->getPosition(&x, &y);
4501
Jeff Brownace13b12011-03-09 17:39:48 -08004502 mPointerGesture.currentGestureIdBits.clear();
4503 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4504 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004505 mPointerGesture.currentGestureProperties[0].clear();
4506 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4507 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004508 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004509 mPointerGesture.currentGestureCoords[0].clear();
4510 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4511 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004512 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4513 down ? 1.0f : 0.0f);
4514
Jeff Brown65fd2512011-08-18 11:20:58 -07004515 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004516 mPointerGesture.resetTap();
4517 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004518 mPointerGesture.tapX = x;
4519 mPointerGesture.tapY = y;
4520 }
Jeff Brownace13b12011-03-09 17:39:48 -08004521 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004522 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4523 // We need to provide feedback for each finger that goes down so we cannot wait
4524 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004525 //
Jeff Brown2352b972011-04-12 22:39:53 -07004526 // The ambiguous case is deciding what to do when there are two fingers down but they
4527 // have not moved enough to determine whether they are part of a drag or part of a
4528 // freeform gesture, or just a press or long-press at the pointer location.
4529 //
4530 // When there are two fingers we start with the PRESS hypothesis and we generate a
4531 // down at the pointer location.
4532 //
4533 // When the two fingers move enough or when additional fingers are added, we make
4534 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004535 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004536
Jeff Brown214eaf42011-05-26 19:17:02 -07004537 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004538 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004539 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004540 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4541 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004542 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004543 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d42011-06-01 12:33:19 -07004544 // Additional pointers have gone down but not yet settled.
4545 // Reset the gesture.
4546#if DEBUG_GESTURES
4547 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004548 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004549 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d42011-06-01 12:33:19 -07004550 * 0.000001f);
4551#endif
4552 *outCancelPreviousGesture = true;
4553 } else {
4554 // Continue previous gesture.
4555 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4556 }
4557
4558 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004559 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4560 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004561 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004562 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004563
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004564 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004565#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004566 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4567 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004568 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004569 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004570#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004571 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4572 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004573 &mPointerGesture.referenceTouchY);
4574 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4575 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004576 }
Jeff Brownace13b12011-03-09 17:39:48 -08004577
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004578 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004579 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004580 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4581 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004582 mPointerGesture.referenceDeltas[id].dx = 0;
4583 mPointerGesture.referenceDeltas[id].dy = 0;
4584 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004585 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004586
4587 // Add delta for all fingers and calculate a common movement delta.
4588 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004589 BitSet32 commonIdBits(mLastFingerIdBits.value
4590 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004591 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4592 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004593 uint32_t id = idBits.clearFirstMarkedBit();
4594 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4595 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004596 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4597 delta.dx += cpd.x - lpd.x;
4598 delta.dy += cpd.y - lpd.y;
4599
4600 if (first) {
4601 commonDeltaX = delta.dx;
4602 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004603 } else {
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004604 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4605 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4606 }
4607 }
Jeff Brownace13b12011-03-09 17:39:48 -08004608
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004609 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4610 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4611 float dist[MAX_POINTER_ID + 1];
4612 int32_t distOverThreshold = 0;
4613 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004614 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004615 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004616 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4617 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004618 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004619 distOverThreshold += 1;
4620 }
4621 }
4622
4623 // Only transition when at least two pointers have moved further than
4624 // the minimum distance threshold.
4625 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004626 if (currentFingerCount > 2) {
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004627 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004628#if DEBUG_GESTURES
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004629 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004630 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004631#endif
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004632 *outCancelPreviousGesture = true;
4633 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4634 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004635 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004636 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004637 uint32_t id1 = idBits.clearFirstMarkedBit();
4638 uint32_t id2 = idBits.firstMarkedBit();
4639 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4640 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4641 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4642 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4643 // There are two pointers but they are too far apart for a SWIPE,
4644 // switch to FREEFORM.
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004645#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004646 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4647 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004648#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004649 *outCancelPreviousGesture = true;
4650 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4651 } else {
4652 // There are two pointers. Wait for both pointers to start moving
4653 // before deciding whether this is a SWIPE or FREEFORM gesture.
4654 float dist1 = dist[id1];
4655 float dist2 = dist[id2];
4656 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4657 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4658 // Calculate the dot product of the displacement vectors.
4659 // When the vectors are oriented in approximately the same direction,
4660 // the angle betweeen them is near zero and the cosine of the angle
4661 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4662 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4663 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004664 float dx1 = delta1.dx * mPointerXZoomScale;
4665 float dy1 = delta1.dy * mPointerYZoomScale;
4666 float dx2 = delta2.dx * mPointerXZoomScale;
4667 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004668 float dot = dx1 * dx2 + dy1 * dy2;
4669 float cosine = dot / (dist1 * dist2); // denominator always > 0
4670 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4671 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004672#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004673 LOGD("Gestures: PRESS transitioned to SWIPE, "
4674 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4675 "cosine %0.3f >= %0.3f",
4676 dist1, mConfig.pointerGestureMultitouchMinDistance,
4677 dist2, mConfig.pointerGestureMultitouchMinDistance,
4678 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004679#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004680 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4681 } else {
4682 // Pointers are moving in different directions. Switch to FREEFORM.
4683#if DEBUG_GESTURES
4684 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4685 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4686 "cosine %0.3f < %0.3f",
4687 dist1, mConfig.pointerGestureMultitouchMinDistance,
4688 dist2, mConfig.pointerGestureMultitouchMinDistance,
4689 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4690#endif
4691 *outCancelPreviousGesture = true;
4692 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4693 }
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004694 }
Jeff Brownace13b12011-03-09 17:39:48 -08004695 }
4696 }
Jeff Brownace13b12011-03-09 17:39:48 -08004697 }
4698 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004699 // Switch from SWIPE to FREEFORM if additional pointers go down.
4700 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004701 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004702#if DEBUG_GESTURES
4703 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004704 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004705#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004706 *outCancelPreviousGesture = true;
4707 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004708 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004709 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004710
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004711 // Move the reference points based on the overall group motion of the fingers
4712 // except in PRESS mode while waiting for a transition to occur.
4713 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4714 && (commonDeltaX || commonDeltaY)) {
4715 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004716 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004717 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004718 delta.dx = 0;
4719 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004720 }
4721
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004722 mPointerGesture.referenceTouchX += commonDeltaX;
4723 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004724
Jeff Brown65fd2512011-08-18 11:20:58 -07004725 commonDeltaX *= mPointerXMovementScale;
4726 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004727
Jeff Brownbe1aa822011-07-27 16:04:54 -07004728 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004729 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004730
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004731 mPointerGesture.referenceGestureX += commonDeltaX;
4732 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004733 }
4734
4735 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004736 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4737 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4738 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004739#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004740 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004741 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004742 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004743#endif
4744 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4745
4746 mPointerGesture.currentGestureIdBits.clear();
4747 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4748 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004749 mPointerGesture.currentGestureProperties[0].clear();
4750 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4751 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004752 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004753 mPointerGesture.currentGestureCoords[0].clear();
4754 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4755 mPointerGesture.referenceGestureX);
4756 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4757 mPointerGesture.referenceGestureY);
4758 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004759 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4760 // FREEFORM mode.
4761#if DEBUG_GESTURES
4762 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4763 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004764 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004765#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004766 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004767
Jeff Brownace13b12011-03-09 17:39:48 -08004768 mPointerGesture.currentGestureIdBits.clear();
4769
4770 BitSet32 mappedTouchIdBits;
4771 BitSet32 usedGestureIdBits;
4772 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4773 // Initially, assign the active gesture id to the active touch point
4774 // if there is one. No other touch id bits are mapped yet.
4775 if (!*outCancelPreviousGesture) {
4776 mappedTouchIdBits.markBit(activeTouchId);
4777 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4778 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4779 mPointerGesture.activeGestureId;
4780 } else {
4781 mPointerGesture.activeGestureId = -1;
4782 }
4783 } else {
4784 // Otherwise, assume we mapped all touches from the previous frame.
4785 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004786 mappedTouchIdBits.value = mLastFingerIdBits.value
4787 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004788 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4789
4790 // Check whether we need to choose a new active gesture id because the
4791 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004792 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4793 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004794 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004795 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004796 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4797 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4798 mPointerGesture.activeGestureId = -1;
4799 break;
4800 }
4801 }
4802 }
4803
4804#if DEBUG_GESTURES
4805 LOGD("Gestures: FREEFORM follow up "
4806 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4807 "activeGestureId=%d",
4808 mappedTouchIdBits.value, usedGestureIdBits.value,
4809 mPointerGesture.activeGestureId);
4810#endif
4811
Jeff Brown65fd2512011-08-18 11:20:58 -07004812 BitSet32 idBits(mCurrentFingerIdBits);
4813 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004814 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004815 uint32_t gestureId;
4816 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004817 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004818 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4819#if DEBUG_GESTURES
4820 LOGD("Gestures: FREEFORM "
4821 "new mapping for touch id %d -> gesture id %d",
4822 touchId, gestureId);
4823#endif
4824 } else {
4825 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4826#if DEBUG_GESTURES
4827 LOGD("Gestures: FREEFORM "
4828 "existing mapping for touch id %d -> gesture id %d",
4829 touchId, gestureId);
4830#endif
4831 }
4832 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4833 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4834
Jeff Brownbe1aa822011-07-27 16:04:54 -07004835 const RawPointerData::Pointer& pointer =
4836 mCurrentRawPointerData.pointerForId(touchId);
4837 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004838 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004839 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004840 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004841 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004842
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004843 mPointerGesture.currentGestureProperties[i].clear();
4844 mPointerGesture.currentGestureProperties[i].id = gestureId;
4845 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004846 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004847 mPointerGesture.currentGestureCoords[i].clear();
4848 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004849 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004850 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004851 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004852 mPointerGesture.currentGestureCoords[i].setAxisValue(
4853 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4854 }
4855
4856 if (mPointerGesture.activeGestureId < 0) {
4857 mPointerGesture.activeGestureId =
4858 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4859#if DEBUG_GESTURES
4860 LOGD("Gestures: FREEFORM new "
4861 "activeGestureId=%d", mPointerGesture.activeGestureId);
4862#endif
4863 }
Jeff Brown2352b972011-04-12 22:39:53 -07004864 }
Jeff Brownace13b12011-03-09 17:39:48 -08004865 }
4866
Jeff Brownbe1aa822011-07-27 16:04:54 -07004867 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004868
Jeff Brownace13b12011-03-09 17:39:48 -08004869#if DEBUG_GESTURES
4870 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004871 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4872 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004873 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004874 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4875 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004876 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004877 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004878 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004879 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004880 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004881 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4882 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4883 id, index, properties.toolType,
4884 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004885 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4886 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4887 }
4888 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004889 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004890 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004891 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004892 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004893 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4894 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4895 id, index, properties.toolType,
4896 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004897 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4898 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4899 }
4900#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004901 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004902}
4903
Jeff Brown65fd2512011-08-18 11:20:58 -07004904void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4905 mPointerSimple.currentCoords.clear();
4906 mPointerSimple.currentProperties.clear();
4907
4908 bool down, hovering;
4909 if (!mCurrentStylusIdBits.isEmpty()) {
4910 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4911 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4912 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4913 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4914 mPointerController->setPosition(x, y);
4915
4916 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4917 down = !hovering;
4918
4919 mPointerController->getPosition(&x, &y);
4920 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4921 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4922 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4923 mPointerSimple.currentProperties.id = 0;
4924 mPointerSimple.currentProperties.toolType =
4925 mCurrentCookedPointerData.pointerProperties[index].toolType;
4926 } else {
4927 down = false;
4928 hovering = false;
4929 }
4930
4931 dispatchPointerSimple(when, policyFlags, down, hovering);
4932}
4933
4934void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
4935 abortPointerSimple(when, policyFlags);
4936}
4937
4938void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
4939 mPointerSimple.currentCoords.clear();
4940 mPointerSimple.currentProperties.clear();
4941
4942 bool down, hovering;
4943 if (!mCurrentMouseIdBits.isEmpty()) {
4944 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
4945 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
4946 if (mLastMouseIdBits.hasBit(id)) {
4947 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
4948 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
4949 - mLastRawPointerData.pointers[lastIndex].x)
4950 * mPointerXMovementScale;
4951 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
4952 - mLastRawPointerData.pointers[lastIndex].y)
4953 * mPointerYMovementScale;
4954
4955 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
4956 mPointerVelocityControl.move(when, &deltaX, &deltaY);
4957
4958 mPointerController->move(deltaX, deltaY);
4959 } else {
4960 mPointerVelocityControl.reset();
4961 }
4962
4963 down = isPointerDown(mCurrentButtonState);
4964 hovering = !down;
4965
4966 float x, y;
4967 mPointerController->getPosition(&x, &y);
4968 mPointerSimple.currentCoords.copyFrom(
4969 mCurrentCookedPointerData.pointerCoords[currentIndex]);
4970 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4971 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4972 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4973 hovering ? 0.0f : 1.0f);
4974 mPointerSimple.currentProperties.id = 0;
4975 mPointerSimple.currentProperties.toolType =
4976 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
4977 } else {
4978 mPointerVelocityControl.reset();
4979
4980 down = false;
4981 hovering = false;
4982 }
4983
4984 dispatchPointerSimple(when, policyFlags, down, hovering);
4985}
4986
4987void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
4988 abortPointerSimple(when, policyFlags);
4989
4990 mPointerVelocityControl.reset();
4991}
4992
4993void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
4994 bool down, bool hovering) {
4995 int32_t metaState = getContext()->getGlobalMetaState();
4996
4997 if (mPointerController != NULL) {
4998 if (down || hovering) {
4999 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5000 mPointerController->clearSpots();
5001 mPointerController->setButtonState(mCurrentButtonState);
5002 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5003 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5004 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5005 }
5006 }
5007
5008 if (mPointerSimple.down && !down) {
5009 mPointerSimple.down = false;
5010
5011 // Send up.
5012 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5013 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5014 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5015 mOrientedXPrecision, mOrientedYPrecision,
5016 mPointerSimple.downTime);
5017 getListener()->notifyMotion(&args);
5018 }
5019
5020 if (mPointerSimple.hovering && !hovering) {
5021 mPointerSimple.hovering = false;
5022
5023 // Send hover exit.
5024 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5025 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5026 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5027 mOrientedXPrecision, mOrientedYPrecision,
5028 mPointerSimple.downTime);
5029 getListener()->notifyMotion(&args);
5030 }
5031
5032 if (down) {
5033 if (!mPointerSimple.down) {
5034 mPointerSimple.down = true;
5035 mPointerSimple.downTime = when;
5036
5037 // Send down.
5038 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5039 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5040 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5041 mOrientedXPrecision, mOrientedYPrecision,
5042 mPointerSimple.downTime);
5043 getListener()->notifyMotion(&args);
5044 }
5045
5046 // Send move.
5047 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5048 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5049 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5050 mOrientedXPrecision, mOrientedYPrecision,
5051 mPointerSimple.downTime);
5052 getListener()->notifyMotion(&args);
5053 }
5054
5055 if (hovering) {
5056 if (!mPointerSimple.hovering) {
5057 mPointerSimple.hovering = true;
5058
5059 // Send hover enter.
5060 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5061 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5062 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5063 mOrientedXPrecision, mOrientedYPrecision,
5064 mPointerSimple.downTime);
5065 getListener()->notifyMotion(&args);
5066 }
5067
5068 // Send hover move.
5069 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5070 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5071 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5072 mOrientedXPrecision, mOrientedYPrecision,
5073 mPointerSimple.downTime);
5074 getListener()->notifyMotion(&args);
5075 }
5076
5077 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5078 float vscroll = mCurrentRawVScroll;
5079 float hscroll = mCurrentRawHScroll;
5080 mWheelYVelocityControl.move(when, NULL, &vscroll);
5081 mWheelXVelocityControl.move(when, &hscroll, NULL);
5082
5083 // Send scroll.
5084 PointerCoords pointerCoords;
5085 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5086 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5087 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5088
5089 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5090 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5091 1, &mPointerSimple.currentProperties, &pointerCoords,
5092 mOrientedXPrecision, mOrientedYPrecision,
5093 mPointerSimple.downTime);
5094 getListener()->notifyMotion(&args);
5095 }
5096
5097 // Save state.
5098 if (down || hovering) {
5099 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5100 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5101 } else {
5102 mPointerSimple.reset();
5103 }
5104}
5105
5106void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5107 mPointerSimple.currentCoords.clear();
5108 mPointerSimple.currentProperties.clear();
5109
5110 dispatchPointerSimple(when, policyFlags, false, false);
5111}
5112
Jeff Brownace13b12011-03-09 17:39:48 -08005113void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005114 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5115 const PointerProperties* properties, const PointerCoords* coords,
5116 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005117 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5118 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005119 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005120 uint32_t pointerCount = 0;
5121 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005122 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005123 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005124 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005125 pointerCoords[pointerCount].copyFrom(coords[index]);
5126
5127 if (changedId >= 0 && id == uint32_t(changedId)) {
5128 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5129 }
5130
5131 pointerCount += 1;
5132 }
5133
Jeff Brownb6110c22011-04-01 16:15:13 -07005134 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005135
5136 if (changedId >= 0 && pointerCount == 1) {
5137 // Replace initial down and final up action.
5138 // We can compare the action without masking off the changed pointer index
5139 // because we know the index is 0.
5140 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5141 action = AMOTION_EVENT_ACTION_DOWN;
5142 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5143 action = AMOTION_EVENT_ACTION_UP;
5144 } else {
5145 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07005146 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005147 }
5148 }
5149
Jeff Brownbe1aa822011-07-27 16:04:54 -07005150 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005151 action, flags, metaState, buttonState, edgeFlags,
5152 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005153 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005154}
5155
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005156bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005157 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005158 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5159 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005160 bool changed = false;
5161 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005162 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005163 uint32_t inIndex = inIdToIndex[id];
5164 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005165
5166 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005167 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005168 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005169 PointerCoords& curOutCoords = outCoords[outIndex];
5170
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005171 if (curInProperties != curOutProperties) {
5172 curOutProperties.copyFrom(curInProperties);
5173 changed = true;
5174 }
5175
Jeff Brownace13b12011-03-09 17:39:48 -08005176 if (curInCoords != curOutCoords) {
5177 curOutCoords.copyFrom(curInCoords);
5178 changed = true;
5179 }
5180 }
5181 return changed;
5182}
5183
5184void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005185 if (mPointerController != NULL) {
5186 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5187 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005188}
5189
Jeff Brownbe1aa822011-07-27 16:04:54 -07005190bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5191 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5192 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005193}
5194
Jeff Brownbe1aa822011-07-27 16:04:54 -07005195const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005196 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005197 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005198 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005199 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005200
5201#if DEBUG_VIRTUAL_KEYS
5202 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
5203 "left=%d, top=%d, right=%d, bottom=%d",
5204 x, y,
5205 virtualKey.keyCode, virtualKey.scanCode,
5206 virtualKey.hitLeft, virtualKey.hitTop,
5207 virtualKey.hitRight, virtualKey.hitBottom);
5208#endif
5209
5210 if (virtualKey.isHit(x, y)) {
5211 return & virtualKey;
5212 }
5213 }
5214
5215 return NULL;
5216}
5217
Jeff Brownbe1aa822011-07-27 16:04:54 -07005218void TouchInputMapper::assignPointerIds() {
5219 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5220 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5221
5222 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005223
5224 if (currentPointerCount == 0) {
5225 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005226 return;
5227 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005228
Jeff Brownbe1aa822011-07-27 16:04:54 -07005229 if (lastPointerCount == 0) {
5230 // All pointers are new.
5231 for (uint32_t i = 0; i < currentPointerCount; i++) {
5232 uint32_t id = i;
5233 mCurrentRawPointerData.pointers[i].id = id;
5234 mCurrentRawPointerData.idToIndex[id] = i;
5235 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5236 }
5237 return;
5238 }
5239
5240 if (currentPointerCount == 1 && lastPointerCount == 1
5241 && mCurrentRawPointerData.pointers[0].toolType
5242 == mLastRawPointerData.pointers[0].toolType) {
5243 // Only one pointer and no change in count so it must have the same id as before.
5244 uint32_t id = mLastRawPointerData.pointers[0].id;
5245 mCurrentRawPointerData.pointers[0].id = id;
5246 mCurrentRawPointerData.idToIndex[id] = 0;
5247 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5248 return;
5249 }
5250
5251 // General case.
5252 // We build a heap of squared euclidean distances between current and last pointers
5253 // associated with the current and last pointer indices. Then, we find the best
5254 // match (by distance) for each current pointer.
5255 // The pointers must have the same tool type but it is possible for them to
5256 // transition from hovering to touching or vice-versa while retaining the same id.
5257 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5258
5259 uint32_t heapSize = 0;
5260 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5261 currentPointerIndex++) {
5262 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5263 lastPointerIndex++) {
5264 const RawPointerData::Pointer& currentPointer =
5265 mCurrentRawPointerData.pointers[currentPointerIndex];
5266 const RawPointerData::Pointer& lastPointer =
5267 mLastRawPointerData.pointers[lastPointerIndex];
5268 if (currentPointer.toolType == lastPointer.toolType) {
5269 int64_t deltaX = currentPointer.x - lastPointer.x;
5270 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005271
5272 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5273
5274 // Insert new element into the heap (sift up).
5275 heap[heapSize].currentPointerIndex = currentPointerIndex;
5276 heap[heapSize].lastPointerIndex = lastPointerIndex;
5277 heap[heapSize].distance = distance;
5278 heapSize += 1;
5279 }
5280 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005281 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005282
Jeff Brownbe1aa822011-07-27 16:04:54 -07005283 // Heapify
5284 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5285 startIndex -= 1;
5286 for (uint32_t parentIndex = startIndex; ;) {
5287 uint32_t childIndex = parentIndex * 2 + 1;
5288 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005289 break;
5290 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005291
5292 if (childIndex + 1 < heapSize
5293 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5294 childIndex += 1;
5295 }
5296
5297 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5298 break;
5299 }
5300
5301 swap(heap[parentIndex], heap[childIndex]);
5302 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005303 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005304 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005305
5306#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07005307 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
5308 for (size_t i = 0; i < heapSize; i++) {
5309 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5310 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5311 heap[i].distance);
5312 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005313#endif
5314
Jeff Brownbe1aa822011-07-27 16:04:54 -07005315 // Pull matches out by increasing order of distance.
5316 // To avoid reassigning pointers that have already been matched, the loop keeps track
5317 // of which last and current pointers have been matched using the matchedXXXBits variables.
5318 // It also tracks the used pointer id bits.
5319 BitSet32 matchedLastBits(0);
5320 BitSet32 matchedCurrentBits(0);
5321 BitSet32 usedIdBits(0);
5322 bool first = true;
5323 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5324 while (heapSize > 0) {
5325 if (first) {
5326 // The first time through the loop, we just consume the root element of
5327 // the heap (the one with smallest distance).
5328 first = false;
5329 } else {
5330 // Previous iterations consumed the root element of the heap.
5331 // Pop root element off of the heap (sift down).
5332 heap[0] = heap[heapSize];
5333 for (uint32_t parentIndex = 0; ;) {
5334 uint32_t childIndex = parentIndex * 2 + 1;
5335 if (childIndex >= heapSize) {
5336 break;
5337 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005338
Jeff Brownbe1aa822011-07-27 16:04:54 -07005339 if (childIndex + 1 < heapSize
5340 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5341 childIndex += 1;
5342 }
5343
5344 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5345 break;
5346 }
5347
5348 swap(heap[parentIndex], heap[childIndex]);
5349 parentIndex = childIndex;
5350 }
5351
5352#if DEBUG_POINTER_ASSIGNMENT
5353 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
5354 for (size_t i = 0; i < heapSize; i++) {
5355 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5356 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5357 heap[i].distance);
5358 }
5359#endif
5360 }
5361
5362 heapSize -= 1;
5363
5364 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5365 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5366
5367 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5368 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5369
5370 matchedCurrentBits.markBit(currentPointerIndex);
5371 matchedLastBits.markBit(lastPointerIndex);
5372
5373 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5374 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5375 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5376 mCurrentRawPointerData.markIdBit(id,
5377 mCurrentRawPointerData.isHovering(currentPointerIndex));
5378 usedIdBits.markBit(id);
5379
5380#if DEBUG_POINTER_ASSIGNMENT
5381 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
5382 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5383#endif
5384 break;
5385 }
5386 }
5387
5388 // Assign fresh ids to pointers that were not matched in the process.
5389 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5390 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5391 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5392
5393 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5394 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5395 mCurrentRawPointerData.markIdBit(id,
5396 mCurrentRawPointerData.isHovering(currentPointerIndex));
5397
5398#if DEBUG_POINTER_ASSIGNMENT
5399 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
5400 currentPointerIndex, id);
5401#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005402 }
5403}
5404
Jeff Brown6d0fec22010-07-23 21:28:06 -07005405int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005406 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5407 return AKEY_STATE_VIRTUAL;
5408 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005409
Jeff Brownbe1aa822011-07-27 16:04:54 -07005410 size_t numVirtualKeys = mVirtualKeys.size();
5411 for (size_t i = 0; i < numVirtualKeys; i++) {
5412 const VirtualKey& virtualKey = mVirtualKeys[i];
5413 if (virtualKey.keyCode == keyCode) {
5414 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005415 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005416 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005417
5418 return AKEY_STATE_UNKNOWN;
5419}
5420
5421int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005422 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5423 return AKEY_STATE_VIRTUAL;
5424 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005425
Jeff Brownbe1aa822011-07-27 16:04:54 -07005426 size_t numVirtualKeys = mVirtualKeys.size();
5427 for (size_t i = 0; i < numVirtualKeys; i++) {
5428 const VirtualKey& virtualKey = mVirtualKeys[i];
5429 if (virtualKey.scanCode == scanCode) {
5430 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005431 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005432 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005433
5434 return AKEY_STATE_UNKNOWN;
5435}
5436
5437bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5438 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005439 size_t numVirtualKeys = mVirtualKeys.size();
5440 for (size_t i = 0; i < numVirtualKeys; i++) {
5441 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005442
Jeff Brownbe1aa822011-07-27 16:04:54 -07005443 for (size_t i = 0; i < numCodes; i++) {
5444 if (virtualKey.keyCode == keyCodes[i]) {
5445 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005446 }
5447 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005448 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005449
5450 return true;
5451}
5452
5453
5454// --- SingleTouchInputMapper ---
5455
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005456SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5457 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005458}
5459
5460SingleTouchInputMapper::~SingleTouchInputMapper() {
5461}
5462
Jeff Brown65fd2512011-08-18 11:20:58 -07005463void SingleTouchInputMapper::reset(nsecs_t when) {
5464 mSingleTouchMotionAccumulator.reset(getDevice());
5465
5466 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005467}
5468
Jeff Brown6d0fec22010-07-23 21:28:06 -07005469void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005470 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005471
Jeff Brown65fd2512011-08-18 11:20:58 -07005472 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005473}
5474
Jeff Brown65fd2512011-08-18 11:20:58 -07005475void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005476 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005477 mCurrentRawPointerData.pointerCount = 1;
5478 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005479
Jeff Brown65fd2512011-08-18 11:20:58 -07005480 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5481 && (mTouchButtonAccumulator.isHovering()
5482 || (mRawPointerAxes.pressure.valid
5483 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005484 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005485
Jeff Brownbe1aa822011-07-27 16:04:54 -07005486 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005487 outPointer.id = 0;
5488 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5489 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5490 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5491 outPointer.touchMajor = 0;
5492 outPointer.touchMinor = 0;
5493 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5494 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5495 outPointer.orientation = 0;
5496 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005497 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5498 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005499 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5500 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5501 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5502 }
5503 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005504 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005505}
5506
Jeff Brownbe1aa822011-07-27 16:04:54 -07005507void SingleTouchInputMapper::configureRawPointerAxes() {
5508 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005509
Jeff Brownbe1aa822011-07-27 16:04:54 -07005510 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5511 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5512 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5513 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5514 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005515 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5516 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005517}
5518
5519
5520// --- MultiTouchInputMapper ---
5521
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005522MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005523 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005524}
5525
5526MultiTouchInputMapper::~MultiTouchInputMapper() {
5527}
5528
Jeff Brown65fd2512011-08-18 11:20:58 -07005529void MultiTouchInputMapper::reset(nsecs_t when) {
5530 mMultiTouchMotionAccumulator.reset(getDevice());
5531
Jeff Brown6894a292011-07-01 17:59:27 -07005532 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005533
Jeff Brown65fd2512011-08-18 11:20:58 -07005534 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005535}
5536
5537void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005538 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005539
Jeff Brown65fd2512011-08-18 11:20:58 -07005540 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005541}
5542
Jeff Brown65fd2512011-08-18 11:20:58 -07005543void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005544 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005545 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005546 BitSet32 newPointerIdBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005547
Jeff Brown80fd47c2011-05-24 01:07:44 -07005548 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005549 const MultiTouchMotionAccumulator::Slot* inSlot =
5550 mMultiTouchMotionAccumulator.getSlot(inIndex);
5551 if (!inSlot->isInUse()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07005552 continue;
5553 }
5554
Jeff Brown80fd47c2011-05-24 01:07:44 -07005555 if (outCount >= MAX_POINTERS) {
5556#if DEBUG_POINTERS
5557 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5558 "ignoring the rest.",
5559 getDeviceName().string(), MAX_POINTERS);
5560#endif
5561 break; // too many fingers!
5562 }
5563
Jeff Brownbe1aa822011-07-27 16:04:54 -07005564 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005565 outPointer.x = inSlot->getX();
5566 outPointer.y = inSlot->getY();
5567 outPointer.pressure = inSlot->getPressure();
5568 outPointer.touchMajor = inSlot->getTouchMajor();
5569 outPointer.touchMinor = inSlot->getTouchMinor();
5570 outPointer.toolMajor = inSlot->getToolMajor();
5571 outPointer.toolMinor = inSlot->getToolMinor();
5572 outPointer.orientation = inSlot->getOrientation();
5573 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005574 outPointer.tiltX = 0;
5575 outPointer.tiltY = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005576
Jeff Brown49754db2011-07-01 17:37:58 -07005577 outPointer.toolType = inSlot->getToolType();
5578 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5579 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5580 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5581 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5582 }
Jeff Brown8d608662010-08-30 03:02:23 -07005583 }
5584
Jeff Brown65fd2512011-08-18 11:20:58 -07005585 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5586 && (mTouchButtonAccumulator.isHovering()
5587 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005588 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005589
Jeff Brown8d608662010-08-30 03:02:23 -07005590 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005591 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005592 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005593 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005594 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005595 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005596 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005597 if (mPointerTrackingIdMap[n] == trackingId) {
5598 id = n;
5599 }
5600 }
5601
5602 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005603 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005604 mPointerTrackingIdMap[id] = trackingId;
5605 }
5606 }
5607 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005608 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005609 mCurrentRawPointerData.clearIdBits();
5610 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005611 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005612 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005613 mCurrentRawPointerData.idToIndex[id] = outCount;
5614 mCurrentRawPointerData.markIdBit(id, isHovering);
5615 newPointerIdBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005616 }
5617 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005618
Jeff Brown6d0fec22010-07-23 21:28:06 -07005619 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005620 }
5621
Jeff Brownbe1aa822011-07-27 16:04:54 -07005622 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005623 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005624
Jeff Brown65fd2512011-08-18 11:20:58 -07005625 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005626}
5627
Jeff Brownbe1aa822011-07-27 16:04:54 -07005628void MultiTouchInputMapper::configureRawPointerAxes() {
5629 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005630
Jeff Brownbe1aa822011-07-27 16:04:54 -07005631 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5632 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5633 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5634 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5635 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5636 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5637 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5638 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5639 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5640 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5641 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005642
Jeff Brownbe1aa822011-07-27 16:04:54 -07005643 if (mRawPointerAxes.trackingId.valid
5644 && mRawPointerAxes.slot.valid
5645 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5646 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005647 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005648 LOGW("MultiTouch Device %s reported %d slots but the framework "
5649 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005650 getDeviceName().string(), slotCount, MAX_SLOTS);
5651 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005652 }
Jeff Brown49754db2011-07-01 17:37:58 -07005653 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005654 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005655 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005656 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005657}
5658
Jeff Brown46b9ac02010-04-22 18:58:52 -07005659
Jeff Browncb1404e2011-01-15 18:14:15 -08005660// --- JoystickInputMapper ---
5661
5662JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5663 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005664}
5665
5666JoystickInputMapper::~JoystickInputMapper() {
5667}
5668
5669uint32_t JoystickInputMapper::getSources() {
5670 return AINPUT_SOURCE_JOYSTICK;
5671}
5672
5673void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5674 InputMapper::populateDeviceInfo(info);
5675
Jeff Brown6f2fba42011-02-19 01:08:02 -08005676 for (size_t i = 0; i < mAxes.size(); i++) {
5677 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005678 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5679 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005680 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005681 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5682 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005683 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005684 }
5685}
5686
5687void JoystickInputMapper::dump(String8& dump) {
5688 dump.append(INDENT2 "Joystick Input Mapper:\n");
5689
Jeff Brown6f2fba42011-02-19 01:08:02 -08005690 dump.append(INDENT3 "Axes:\n");
5691 size_t numAxes = mAxes.size();
5692 for (size_t i = 0; i < numAxes; i++) {
5693 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005694 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005695 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005696 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005697 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005698 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005699 }
Jeff Brown85297452011-03-04 13:07:49 -08005700 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5701 label = getAxisLabel(axis.axisInfo.highAxis);
5702 if (label) {
5703 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5704 } else {
5705 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5706 axis.axisInfo.splitValue);
5707 }
5708 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5709 dump.append(" (invert)");
5710 }
5711
5712 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5713 axis.min, axis.max, axis.flat, axis.fuzz);
5714 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5715 "highScale=%0.5f, highOffset=%0.5f\n",
5716 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005717 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5718 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005719 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005720 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005721 }
5722}
5723
Jeff Brown65fd2512011-08-18 11:20:58 -07005724void JoystickInputMapper::configure(nsecs_t when,
5725 const InputReaderConfiguration* config, uint32_t changes) {
5726 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005727
Jeff Brown474dcb52011-06-14 20:22:50 -07005728 if (!changes) { // first time only
5729 // Collect all axes.
5730 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5731 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005732 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005733 if (rawAxisInfo.valid) {
5734 // Map axis.
5735 AxisInfo axisInfo;
5736 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5737 if (!explicitlyMapped) {
5738 // Axis is not explicitly mapped, will choose a generic axis later.
5739 axisInfo.mode = AxisInfo::MODE_NORMAL;
5740 axisInfo.axis = -1;
5741 }
5742
5743 // Apply flat override.
5744 int32_t rawFlat = axisInfo.flatOverride < 0
5745 ? rawAxisInfo.flat : axisInfo.flatOverride;
5746
5747 // Calculate scaling factors and limits.
5748 Axis axis;
5749 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5750 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5751 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5752 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5753 scale, 0.0f, highScale, 0.0f,
5754 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5755 } else if (isCenteredAxis(axisInfo.axis)) {
5756 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5757 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5758 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5759 scale, offset, scale, offset,
5760 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5761 } else {
5762 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5763 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5764 scale, 0.0f, scale, 0.0f,
5765 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5766 }
5767
5768 // To eliminate noise while the joystick is at rest, filter out small variations
5769 // in axis values up front.
5770 axis.filter = axis.flat * 0.25f;
5771
5772 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005773 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005774 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005775
Jeff Brown474dcb52011-06-14 20:22:50 -07005776 // If there are too many axes, start dropping them.
5777 // Prefer to keep explicitly mapped axes.
5778 if (mAxes.size() > PointerCoords::MAX_AXES) {
5779 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5780 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5781 pruneAxes(true);
5782 pruneAxes(false);
5783 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005784
Jeff Brown474dcb52011-06-14 20:22:50 -07005785 // Assign generic axis ids to remaining axes.
5786 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5787 size_t numAxes = mAxes.size();
5788 for (size_t i = 0; i < numAxes; i++) {
5789 Axis& axis = mAxes.editValueAt(i);
5790 if (axis.axisInfo.axis < 0) {
5791 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5792 && haveAxis(nextGenericAxisId)) {
5793 nextGenericAxisId += 1;
5794 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005795
Jeff Brown474dcb52011-06-14 20:22:50 -07005796 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5797 axis.axisInfo.axis = nextGenericAxisId;
5798 nextGenericAxisId += 1;
5799 } else {
5800 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5801 "have already been assigned to other axes.",
5802 getDeviceName().string(), mAxes.keyAt(i));
5803 mAxes.removeItemsAt(i--);
5804 numAxes -= 1;
5805 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005806 }
5807 }
5808 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005809}
5810
Jeff Brown85297452011-03-04 13:07:49 -08005811bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005812 size_t numAxes = mAxes.size();
5813 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005814 const Axis& axis = mAxes.valueAt(i);
5815 if (axis.axisInfo.axis == axisId
5816 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5817 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005818 return true;
5819 }
5820 }
5821 return false;
5822}
Jeff Browncb1404e2011-01-15 18:14:15 -08005823
Jeff Brown6f2fba42011-02-19 01:08:02 -08005824void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5825 size_t i = mAxes.size();
5826 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5827 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5828 continue;
5829 }
5830 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5831 getDeviceName().string(), mAxes.keyAt(i));
5832 mAxes.removeItemsAt(i);
5833 }
5834}
5835
5836bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5837 switch (axis) {
5838 case AMOTION_EVENT_AXIS_X:
5839 case AMOTION_EVENT_AXIS_Y:
5840 case AMOTION_EVENT_AXIS_Z:
5841 case AMOTION_EVENT_AXIS_RX:
5842 case AMOTION_EVENT_AXIS_RY:
5843 case AMOTION_EVENT_AXIS_RZ:
5844 case AMOTION_EVENT_AXIS_HAT_X:
5845 case AMOTION_EVENT_AXIS_HAT_Y:
5846 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005847 case AMOTION_EVENT_AXIS_RUDDER:
5848 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005849 return true;
5850 default:
5851 return false;
5852 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005853}
5854
Jeff Brown65fd2512011-08-18 11:20:58 -07005855void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005856 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005857 size_t numAxes = mAxes.size();
5858 for (size_t i = 0; i < numAxes; i++) {
5859 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005860 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005861 }
5862
Jeff Brown65fd2512011-08-18 11:20:58 -07005863 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005864}
5865
5866void JoystickInputMapper::process(const RawEvent* rawEvent) {
5867 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005868 case EV_ABS: {
5869 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5870 if (index >= 0) {
5871 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005872 float newValue, highNewValue;
5873 switch (axis.axisInfo.mode) {
5874 case AxisInfo::MODE_INVERT:
5875 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5876 * axis.scale + axis.offset;
5877 highNewValue = 0.0f;
5878 break;
5879 case AxisInfo::MODE_SPLIT:
5880 if (rawEvent->value < axis.axisInfo.splitValue) {
5881 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5882 * axis.scale + axis.offset;
5883 highNewValue = 0.0f;
5884 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5885 newValue = 0.0f;
5886 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5887 * axis.highScale + axis.highOffset;
5888 } else {
5889 newValue = 0.0f;
5890 highNewValue = 0.0f;
5891 }
5892 break;
5893 default:
5894 newValue = rawEvent->value * axis.scale + axis.offset;
5895 highNewValue = 0.0f;
5896 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005897 }
Jeff Brown85297452011-03-04 13:07:49 -08005898 axis.newValue = newValue;
5899 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005900 }
5901 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005902 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005903
5904 case EV_SYN:
5905 switch (rawEvent->scanCode) {
5906 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005907 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005908 break;
5909 }
5910 break;
5911 }
5912}
5913
Jeff Brown6f2fba42011-02-19 01:08:02 -08005914void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005915 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005916 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005917 }
5918
5919 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005920 int32_t buttonState = 0;
5921
5922 PointerProperties pointerProperties;
5923 pointerProperties.clear();
5924 pointerProperties.id = 0;
5925 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005926
Jeff Brown6f2fba42011-02-19 01:08:02 -08005927 PointerCoords pointerCoords;
5928 pointerCoords.clear();
5929
5930 size_t numAxes = mAxes.size();
5931 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005932 const Axis& axis = mAxes.valueAt(i);
5933 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5934 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5935 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5936 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005937 }
5938
Jeff Brown56194eb2011-03-02 19:23:13 -08005939 // Moving a joystick axis should not wake the devide because joysticks can
5940 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5941 // button will likely wake the device.
5942 // TODO: Use the input device configuration to control this behavior more finely.
5943 uint32_t policyFlags = 0;
5944
Jeff Brownbe1aa822011-07-27 16:04:54 -07005945 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005946 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5947 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005948 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005949}
5950
Jeff Brown85297452011-03-04 13:07:49 -08005951bool JoystickInputMapper::filterAxes(bool force) {
5952 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005953 size_t numAxes = mAxes.size();
5954 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005955 Axis& axis = mAxes.editValueAt(i);
5956 if (force || hasValueChangedSignificantly(axis.filter,
5957 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5958 axis.currentValue = axis.newValue;
5959 atLeastOneSignificantChange = true;
5960 }
5961 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5962 if (force || hasValueChangedSignificantly(axis.filter,
5963 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5964 axis.highCurrentValue = axis.highNewValue;
5965 atLeastOneSignificantChange = true;
5966 }
5967 }
5968 }
5969 return atLeastOneSignificantChange;
5970}
5971
5972bool JoystickInputMapper::hasValueChangedSignificantly(
5973 float filter, float newValue, float currentValue, float min, float max) {
5974 if (newValue != currentValue) {
5975 // Filter out small changes in value unless the value is converging on the axis
5976 // bounds or center point. This is intended to reduce the amount of information
5977 // sent to applications by particularly noisy joysticks (such as PS3).
5978 if (fabs(newValue - currentValue) > filter
5979 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5980 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5981 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5982 return true;
5983 }
5984 }
5985 return false;
5986}
5987
5988bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5989 float filter, float newValue, float currentValue, float thresholdValue) {
5990 float newDistance = fabs(newValue - thresholdValue);
5991 if (newDistance < filter) {
5992 float oldDistance = fabs(currentValue - thresholdValue);
5993 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005994 return true;
5995 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005996 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005997 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005998}
5999
Jeff Brown46b9ac02010-04-22 18:58:52 -07006000} // namespace android