blob: 40c85fc7f0535b349af3cb13be67fdb52f557c77 [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;
Jeff Brown65fd2512011-08-18 11:20:58 -07003397 }
3398
3399 dispatchPointerUsage(when, policyFlags, pointerUsage);
3400 } else {
3401 dispatchHoverExit(when, policyFlags);
3402 dispatchTouches(when, policyFlags);
3403 dispatchHoverEnterAndMove(when, policyFlags);
3404 }
3405
3406 // Synthesize key up from raw buttons if needed.
3407 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3408 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003409 }
3410
Jeff Brown6328cdc2010-07-29 18:18:33 -07003411 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003412 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3413 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3414 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003415 mLastFingerIdBits = mCurrentFingerIdBits;
3416 mLastStylusIdBits = mCurrentStylusIdBits;
3417 mLastMouseIdBits = mCurrentMouseIdBits;
3418
3419 // Clear some transient state.
3420 mCurrentRawVScroll = 0;
3421 mCurrentRawHScroll = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003422}
3423
Jeff Brown79ac9692011-04-19 21:20:10 -07003424void TouchInputMapper::timeoutExpired(nsecs_t when) {
3425 if (mPointerController != NULL) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003426 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3427 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3428 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003429 }
3430}
3431
Jeff Brownbe1aa822011-07-27 16:04:54 -07003432bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3433 // Check for release of a virtual key.
3434 if (mCurrentVirtualKey.down) {
3435 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3436 // Pointer went up while virtual key was down.
3437 mCurrentVirtualKey.down = false;
3438 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003439#if DEBUG_VIRTUAL_KEYS
3440 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003441 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003442#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003443 dispatchVirtualKey(when, policyFlags,
3444 AKEY_EVENT_ACTION_UP,
3445 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003446 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003447 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003448 }
3449
Jeff Brownbe1aa822011-07-27 16:04:54 -07003450 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3451 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3452 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3453 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3454 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3455 // Pointer is still within the space of the virtual key.
3456 return true;
3457 }
3458 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003459
Jeff Brownbe1aa822011-07-27 16:04:54 -07003460 // Pointer left virtual key area or another pointer also went down.
3461 // Send key cancellation but do not consume the touch yet.
3462 // This is useful when the user swipes through from the virtual key area
3463 // into the main display surface.
3464 mCurrentVirtualKey.down = false;
3465 if (!mCurrentVirtualKey.ignored) {
3466#if DEBUG_VIRTUAL_KEYS
3467 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3468 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3469#endif
3470 dispatchVirtualKey(when, policyFlags,
3471 AKEY_EVENT_ACTION_UP,
3472 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3473 | AKEY_EVENT_FLAG_CANCELED);
3474 }
3475 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003476
Jeff Brownbe1aa822011-07-27 16:04:54 -07003477 if (mLastRawPointerData.touchingIdBits.isEmpty()
3478 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3479 // Pointer just went down. Check for virtual key press or off-screen touches.
3480 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3481 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3482 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3483 // If exactly one pointer went down, check for virtual key hit.
3484 // Otherwise we will drop the entire stroke.
3485 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3486 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3487 if (virtualKey) {
3488 mCurrentVirtualKey.down = true;
3489 mCurrentVirtualKey.downTime = when;
3490 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3491 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3492 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3493 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3494
3495 if (!mCurrentVirtualKey.ignored) {
3496#if DEBUG_VIRTUAL_KEYS
3497 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3498 mCurrentVirtualKey.keyCode,
3499 mCurrentVirtualKey.scanCode);
3500#endif
3501 dispatchVirtualKey(when, policyFlags,
3502 AKEY_EVENT_ACTION_DOWN,
3503 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3504 }
3505 }
3506 }
3507 return true;
3508 }
3509 }
3510
Jeff Brownfe508922011-01-18 15:10:10 -08003511 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003512 // most recent touch within the screen area. The idea is to filter out stray
3513 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003514 //
3515 // Problems we're trying to solve:
3516 //
3517 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3518 // virtual key area that is implemented by a separate touch panel and accidentally
3519 // triggers a virtual key.
3520 //
3521 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3522 // area and accidentally triggers a virtual key. This often happens when virtual keys
3523 // are layed out below the screen near to where the on screen keyboard's space bar
3524 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003525 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003526 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003527 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003528 return false;
3529}
3530
3531void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3532 int32_t keyEventAction, int32_t keyEventFlags) {
3533 int32_t keyCode = mCurrentVirtualKey.keyCode;
3534 int32_t scanCode = mCurrentVirtualKey.scanCode;
3535 nsecs_t downTime = mCurrentVirtualKey.downTime;
3536 int32_t metaState = mContext->getGlobalMetaState();
3537 policyFlags |= POLICY_FLAG_VIRTUAL;
3538
3539 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3540 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3541 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003542}
3543
Jeff Brown6d0fec22010-07-23 21:28:06 -07003544void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003545 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3546 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003547 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003548 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003549
3550 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003551 if (!currentIdBits.isEmpty()) {
3552 // No pointer id changes so this is a move event.
3553 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003554 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003555 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3556 AMOTION_EVENT_EDGE_FLAG_NONE,
3557 mCurrentCookedPointerData.pointerProperties,
3558 mCurrentCookedPointerData.pointerCoords,
3559 mCurrentCookedPointerData.idToIndex,
3560 currentIdBits, -1,
3561 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3562 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003563 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003564 // There may be pointers going up and pointers going down and pointers moving
3565 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003566 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3567 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003568 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003569 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003570
Jeff Brownace13b12011-03-09 17:39:48 -08003571 // Update last coordinates of pointers that have moved so that we observe the new
3572 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003573 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003574 mCurrentCookedPointerData.pointerProperties,
3575 mCurrentCookedPointerData.pointerCoords,
3576 mCurrentCookedPointerData.idToIndex,
3577 mLastCookedPointerData.pointerProperties,
3578 mLastCookedPointerData.pointerCoords,
3579 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003580 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003581 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003582 moveNeeded = true;
3583 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003584
Jeff Brownace13b12011-03-09 17:39:48 -08003585 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003586 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003587 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003588
Jeff Brown65fd2512011-08-18 11:20:58 -07003589 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003590 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003591 mLastCookedPointerData.pointerProperties,
3592 mLastCookedPointerData.pointerCoords,
3593 mLastCookedPointerData.idToIndex,
3594 dispatchedIdBits, upId,
3595 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003596 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003597 }
3598
Jeff Brownc3db8582010-10-20 15:33:38 -07003599 // Dispatch move events if any of the remaining pointers moved from their old locations.
3600 // Although applications receive new locations as part of individual pointer up
3601 // events, they do not generally handle them except when presented in a move event.
3602 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003603 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003604 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003605 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003606 mCurrentCookedPointerData.pointerProperties,
3607 mCurrentCookedPointerData.pointerCoords,
3608 mCurrentCookedPointerData.idToIndex,
3609 dispatchedIdBits, -1,
3610 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003611 }
3612
3613 // Dispatch pointer down events using the new pointer locations.
3614 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003615 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003616 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003617
Jeff Brownace13b12011-03-09 17:39:48 -08003618 if (dispatchedIdBits.count() == 1) {
3619 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003620 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003621 }
3622
Jeff Brown65fd2512011-08-18 11:20:58 -07003623 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003624 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003625 mCurrentCookedPointerData.pointerProperties,
3626 mCurrentCookedPointerData.pointerCoords,
3627 mCurrentCookedPointerData.idToIndex,
3628 dispatchedIdBits, downId,
3629 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003630 }
3631 }
Jeff Brownace13b12011-03-09 17:39:48 -08003632}
3633
Jeff Brownbe1aa822011-07-27 16:04:54 -07003634void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3635 if (mSentHoverEnter &&
3636 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3637 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3638 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003639 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003640 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3641 mLastCookedPointerData.pointerProperties,
3642 mLastCookedPointerData.pointerCoords,
3643 mLastCookedPointerData.idToIndex,
3644 mLastCookedPointerData.hoveringIdBits, -1,
3645 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3646 mSentHoverEnter = false;
3647 }
3648}
Jeff Brownace13b12011-03-09 17:39:48 -08003649
Jeff Brownbe1aa822011-07-27 16:04:54 -07003650void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3651 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3652 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3653 int32_t metaState = getContext()->getGlobalMetaState();
3654 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003655 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003656 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3657 mCurrentCookedPointerData.pointerProperties,
3658 mCurrentCookedPointerData.pointerCoords,
3659 mCurrentCookedPointerData.idToIndex,
3660 mCurrentCookedPointerData.hoveringIdBits, -1,
3661 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3662 mSentHoverEnter = true;
3663 }
Jeff Brownace13b12011-03-09 17:39:48 -08003664
Jeff Brown65fd2512011-08-18 11:20:58 -07003665 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003666 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3667 mCurrentCookedPointerData.pointerProperties,
3668 mCurrentCookedPointerData.pointerCoords,
3669 mCurrentCookedPointerData.idToIndex,
3670 mCurrentCookedPointerData.hoveringIdBits, -1,
3671 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3672 }
3673}
3674
3675void TouchInputMapper::cookPointerData() {
3676 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3677
3678 mCurrentCookedPointerData.clear();
3679 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3680 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3681 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3682
3683 // Walk through the the active pointers and map device coordinates onto
3684 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003685 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003686 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003687
Jeff Browna1f89ce2011-08-11 00:05:01 -07003688 // Size
3689 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3690 switch (mCalibration.sizeCalibration) {
3691 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3692 case Calibration::SIZE_CALIBRATION_DIAMETER:
3693 case Calibration::SIZE_CALIBRATION_AREA:
3694 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3695 touchMajor = in.touchMajor;
3696 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3697 toolMajor = in.toolMajor;
3698 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3699 size = mRawPointerAxes.touchMinor.valid
3700 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3701 } else if (mRawPointerAxes.touchMajor.valid) {
3702 toolMajor = touchMajor = in.touchMajor;
3703 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3704 ? in.touchMinor : in.touchMajor;
3705 size = mRawPointerAxes.touchMinor.valid
3706 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3707 } else if (mRawPointerAxes.toolMajor.valid) {
3708 touchMajor = toolMajor = in.toolMajor;
3709 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3710 ? in.toolMinor : in.toolMajor;
3711 size = mRawPointerAxes.toolMinor.valid
3712 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003713 } else {
Jeff Browna1f89ce2011-08-11 00:05:01 -07003714 LOG_ASSERT(false, "No touch or tool axes. "
3715 "Size calibration should have been resolved to NONE.");
3716 touchMajor = 0;
3717 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003718 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003719 toolMinor = 0;
3720 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003721 }
Jeff Brownace13b12011-03-09 17:39:48 -08003722
Jeff Browna1f89ce2011-08-11 00:05:01 -07003723 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3724 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3725 if (touchingCount > 1) {
3726 touchMajor /= touchingCount;
3727 touchMinor /= touchingCount;
3728 toolMajor /= touchingCount;
3729 toolMinor /= touchingCount;
3730 size /= touchingCount;
3731 }
3732 }
Jeff Brownace13b12011-03-09 17:39:48 -08003733
Jeff Browna1f89ce2011-08-11 00:05:01 -07003734 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3735 touchMajor *= mGeometricScale;
3736 touchMinor *= mGeometricScale;
3737 toolMajor *= mGeometricScale;
3738 toolMinor *= mGeometricScale;
3739 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3740 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003741 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003742 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3743 toolMinor = toolMajor;
3744 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3745 touchMinor = touchMajor;
3746 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003747 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003748
3749 mCalibration.applySizeScaleAndBias(&touchMajor);
3750 mCalibration.applySizeScaleAndBias(&touchMinor);
3751 mCalibration.applySizeScaleAndBias(&toolMajor);
3752 mCalibration.applySizeScaleAndBias(&toolMinor);
3753 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003754 break;
3755 default:
3756 touchMajor = 0;
3757 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003758 toolMajor = 0;
3759 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003760 size = 0;
3761 break;
3762 }
3763
Jeff Browna1f89ce2011-08-11 00:05:01 -07003764 // Pressure
3765 float pressure;
3766 switch (mCalibration.pressureCalibration) {
3767 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3768 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3769 pressure = in.pressure * mPressureScale;
3770 break;
3771 default:
3772 pressure = in.isHovering ? 0 : 1;
3773 break;
3774 }
3775
Jeff Brown65fd2512011-08-18 11:20:58 -07003776 // Tilt and Orientation
3777 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003778 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003779 if (mHaveTilt) {
3780 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3781 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3782 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3783 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3784 } else {
3785 tilt = 0;
3786
3787 switch (mCalibration.orientationCalibration) {
3788 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3789 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3790 break;
3791 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3792 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3793 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3794 if (c1 != 0 || c2 != 0) {
3795 orientation = atan2f(c1, c2) * 0.5f;
3796 float confidence = hypotf(c1, c2);
3797 float scale = 1.0f + confidence / 16.0f;
3798 touchMajor *= scale;
3799 touchMinor /= scale;
3800 toolMajor *= scale;
3801 toolMinor /= scale;
3802 } else {
3803 orientation = 0;
3804 }
3805 break;
3806 }
3807 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003808 orientation = 0;
3809 }
Jeff Brownace13b12011-03-09 17:39:48 -08003810 }
3811
Jeff Brown80fd47c2011-05-24 01:07:44 -07003812 // Distance
3813 float distance;
3814 switch (mCalibration.distanceCalibration) {
3815 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003816 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003817 break;
3818 default:
3819 distance = 0;
3820 }
3821
Jeff Brownace13b12011-03-09 17:39:48 -08003822 // X and Y
3823 // Adjust coords for surface orientation.
3824 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003825 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003826 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003827 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3828 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003829 orientation -= M_PI_2;
3830 if (orientation < - M_PI_2) {
3831 orientation += M_PI;
3832 }
3833 break;
3834 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003835 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3836 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003837 break;
3838 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003839 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3840 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003841 orientation += M_PI_2;
3842 if (orientation > M_PI_2) {
3843 orientation -= M_PI;
3844 }
3845 break;
3846 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003847 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3848 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003849 break;
3850 }
3851
3852 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003853 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003854 out.clear();
3855 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3856 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3857 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3858 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3859 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3860 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3861 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3862 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3863 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003864 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003865 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003866
3867 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003868 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3869 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003870 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003871 properties.id = id;
3872 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003873
Jeff Brownbe1aa822011-07-27 16:04:54 -07003874 // Write id index.
3875 mCurrentCookedPointerData.idToIndex[id] = i;
3876 }
Jeff Brownace13b12011-03-09 17:39:48 -08003877}
3878
Jeff Brown65fd2512011-08-18 11:20:58 -07003879void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3880 PointerUsage pointerUsage) {
3881 if (pointerUsage != mPointerUsage) {
3882 abortPointerUsage(when, policyFlags);
3883 mPointerUsage = pointerUsage;
3884 }
3885
3886 switch (mPointerUsage) {
3887 case POINTER_USAGE_GESTURES:
3888 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3889 break;
3890 case POINTER_USAGE_STYLUS:
3891 dispatchPointerStylus(when, policyFlags);
3892 break;
3893 case POINTER_USAGE_MOUSE:
3894 dispatchPointerMouse(when, policyFlags);
3895 break;
3896 default:
3897 break;
3898 }
3899}
3900
3901void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3902 switch (mPointerUsage) {
3903 case POINTER_USAGE_GESTURES:
3904 abortPointerGestures(when, policyFlags);
3905 break;
3906 case POINTER_USAGE_STYLUS:
3907 abortPointerStylus(when, policyFlags);
3908 break;
3909 case POINTER_USAGE_MOUSE:
3910 abortPointerMouse(when, policyFlags);
3911 break;
3912 default:
3913 break;
3914 }
3915
3916 mPointerUsage = POINTER_USAGE_NONE;
3917}
3918
Jeff Brown79ac9692011-04-19 21:20:10 -07003919void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3920 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003921 // Update current gesture coordinates.
3922 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003923 bool sendEvents = preparePointerGestures(when,
3924 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3925 if (!sendEvents) {
3926 return;
3927 }
Jeff Brown19c97d42011-06-01 12:33:19 -07003928 if (finishPreviousGesture) {
3929 cancelPreviousGesture = false;
3930 }
Jeff Brownace13b12011-03-09 17:39:48 -08003931
Jeff Brownbb3fcba2011-06-06 19:23:05 -07003932 // Update the pointer presentation and spots.
3933 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3934 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3935 if (finishPreviousGesture || cancelPreviousGesture) {
3936 mPointerController->clearSpots();
3937 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003938 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3939 mPointerGesture.currentGestureIdToIndex,
3940 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07003941 } else {
3942 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3943 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003944
Jeff Brown538881e2011-05-25 18:23:38 -07003945 // Show or hide the pointer if needed.
3946 switch (mPointerGesture.currentGestureMode) {
3947 case PointerGesture::NEUTRAL:
3948 case PointerGesture::QUIET:
3949 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3950 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3951 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3952 // Remind the user of where the pointer is after finishing a gesture with spots.
3953 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3954 }
3955 break;
3956 case PointerGesture::TAP:
3957 case PointerGesture::TAP_DRAG:
3958 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3959 case PointerGesture::HOVER:
3960 case PointerGesture::PRESS:
3961 // Unfade the pointer when the current gesture manipulates the
3962 // area directly under the pointer.
3963 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3964 break;
3965 case PointerGesture::SWIPE:
3966 case PointerGesture::FREEFORM:
3967 // Fade the pointer when the current gesture manipulates a different
3968 // area and there are spots to guide the user experience.
3969 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3970 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3971 } else {
3972 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3973 }
3974 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003975 }
3976
Jeff Brownace13b12011-03-09 17:39:48 -08003977 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003978 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003979 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003980
3981 // Update last coordinates of pointers that have moved so that we observe the new
3982 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003983 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3984 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3985 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003986 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003987 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3988 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3989 bool moveNeeded = false;
3990 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003991 && !mPointerGesture.lastGestureIdBits.isEmpty()
3992 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003993 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3994 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003995 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003996 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003997 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003998 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3999 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004000 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004001 moveNeeded = true;
4002 }
Jeff Brownace13b12011-03-09 17:39:48 -08004003 }
4004
4005 // Send motion events for all pointers that went up or were canceled.
4006 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4007 if (!dispatchedGestureIdBits.isEmpty()) {
4008 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004009 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004010 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4011 AMOTION_EVENT_EDGE_FLAG_NONE,
4012 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004013 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4014 dispatchedGestureIdBits, -1,
4015 0, 0, mPointerGesture.downTime);
4016
4017 dispatchedGestureIdBits.clear();
4018 } else {
4019 BitSet32 upGestureIdBits;
4020 if (finishPreviousGesture) {
4021 upGestureIdBits = dispatchedGestureIdBits;
4022 } else {
4023 upGestureIdBits.value = dispatchedGestureIdBits.value
4024 & ~mPointerGesture.currentGestureIdBits.value;
4025 }
4026 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004027 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004028
Jeff Brown65fd2512011-08-18 11:20:58 -07004029 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004030 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004031 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4032 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004033 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4034 dispatchedGestureIdBits, id,
4035 0, 0, mPointerGesture.downTime);
4036
4037 dispatchedGestureIdBits.clearBit(id);
4038 }
4039 }
4040 }
4041
4042 // Send motion events for all pointers that moved.
4043 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004044 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004045 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4046 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004047 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4048 dispatchedGestureIdBits, -1,
4049 0, 0, mPointerGesture.downTime);
4050 }
4051
4052 // Send motion events for all pointers that went down.
4053 if (down) {
4054 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4055 & ~dispatchedGestureIdBits.value);
4056 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004057 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004058 dispatchedGestureIdBits.markBit(id);
4059
Jeff Brownace13b12011-03-09 17:39:48 -08004060 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004061 mPointerGesture.downTime = when;
4062 }
4063
Jeff Brown65fd2512011-08-18 11:20:58 -07004064 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004065 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004066 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004067 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4068 dispatchedGestureIdBits, id,
4069 0, 0, mPointerGesture.downTime);
4070 }
4071 }
4072
Jeff Brownace13b12011-03-09 17:39:48 -08004073 // Send motion events for hover.
4074 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004075 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004076 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4077 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4078 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004079 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4080 mPointerGesture.currentGestureIdBits, -1,
4081 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004082 } else if (dispatchedGestureIdBits.isEmpty()
4083 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4084 // Synthesize a hover move event after all pointers go up to indicate that
4085 // the pointer is hovering again even if the user is not currently touching
4086 // the touch pad. This ensures that a view will receive a fresh hover enter
4087 // event after a tap.
4088 float x, y;
4089 mPointerController->getPosition(&x, &y);
4090
4091 PointerProperties pointerProperties;
4092 pointerProperties.clear();
4093 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004094 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004095
4096 PointerCoords pointerCoords;
4097 pointerCoords.clear();
4098 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4099 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4100
Jeff Brown65fd2512011-08-18 11:20:58 -07004101 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004102 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4103 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4104 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004105 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004106 }
4107
4108 // Update state.
4109 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4110 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004111 mPointerGesture.lastGestureIdBits.clear();
4112 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004113 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4114 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004115 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004116 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004117 mPointerGesture.lastGestureProperties[index].copyFrom(
4118 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004119 mPointerGesture.lastGestureCoords[index].copyFrom(
4120 mPointerGesture.currentGestureCoords[index]);
4121 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004122 }
4123 }
4124}
4125
Jeff Brown65fd2512011-08-18 11:20:58 -07004126void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4127 // Cancel previously dispatches pointers.
4128 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4129 int32_t metaState = getContext()->getGlobalMetaState();
4130 int32_t buttonState = mCurrentButtonState;
4131 dispatchMotion(when, policyFlags, mSource,
4132 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4133 AMOTION_EVENT_EDGE_FLAG_NONE,
4134 mPointerGesture.lastGestureProperties,
4135 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4136 mPointerGesture.lastGestureIdBits, -1,
4137 0, 0, mPointerGesture.downTime);
4138 }
4139
4140 // Reset the current pointer gesture.
4141 mPointerGesture.reset();
4142 mPointerVelocityControl.reset();
4143
4144 // Remove any current spots.
4145 if (mPointerController != NULL) {
4146 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4147 mPointerController->clearSpots();
4148 }
4149}
4150
Jeff Brown79ac9692011-04-19 21:20:10 -07004151bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4152 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004153 *outCancelPreviousGesture = false;
4154 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004155
Jeff Brown79ac9692011-04-19 21:20:10 -07004156 // Handle TAP timeout.
4157 if (isTimeout) {
4158#if DEBUG_GESTURES
4159 LOGD("Gestures: Processing timeout");
4160#endif
4161
4162 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004163 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004164 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004165 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004166 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004167 } else {
4168 // The tap is finished.
4169#if DEBUG_GESTURES
4170 LOGD("Gestures: TAP finished");
4171#endif
4172 *outFinishPreviousGesture = true;
4173
4174 mPointerGesture.activeGestureId = -1;
4175 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4176 mPointerGesture.currentGestureIdBits.clear();
4177
Jeff Brown65fd2512011-08-18 11:20:58 -07004178 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004179 return true;
4180 }
4181 }
4182
4183 // We did not handle this timeout.
4184 return false;
4185 }
4186
Jeff Brown65fd2512011-08-18 11:20:58 -07004187 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4188 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4189
Jeff Brownace13b12011-03-09 17:39:48 -08004190 // Update the velocity tracker.
4191 {
4192 VelocityTracker::Position positions[MAX_POINTERS];
4193 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004194 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004195 uint32_t id = idBits.clearFirstMarkedBit();
4196 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004197 positions[count].x = pointer.x * mPointerXMovementScale;
4198 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004199 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004200 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004201 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004202 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004203
Jeff Brownace13b12011-03-09 17:39:48 -08004204 // Pick a new active touch id if needed.
4205 // Choose an arbitrary pointer that just went down, if there is one.
4206 // Otherwise choose an arbitrary remaining pointer.
4207 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004208 // We keep the same active touch id for as long as possible.
4209 bool activeTouchChanged = false;
4210 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4211 int32_t activeTouchId = lastActiveTouchId;
4212 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004213 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004214 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004215 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004216 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004217 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004218 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004219 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004220 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004221 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004222 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004223 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004224 } else {
4225 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004226 }
4227 }
4228
4229 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004230 bool isQuietTime = false;
4231 if (activeTouchId < 0) {
4232 mPointerGesture.resetQuietTime();
4233 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004234 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004235 if (!isQuietTime) {
4236 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4237 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4238 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004239 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004240 // Enter quiet time when exiting swipe or freeform state.
4241 // This is to prevent accidentally entering the hover state and flinging the
4242 // pointer when finishing a swipe and there is still one pointer left onscreen.
4243 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004244 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004245 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004246 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004247 // Enter quiet time when releasing the button and there are still two or more
4248 // fingers down. This may indicate that one finger was used to press the button
4249 // but it has not gone up yet.
4250 isQuietTime = true;
4251 }
4252 if (isQuietTime) {
4253 mPointerGesture.quietTime = when;
4254 }
Jeff Brownace13b12011-03-09 17:39:48 -08004255 }
4256 }
4257
4258 // Switch states based on button and pointer state.
4259 if (isQuietTime) {
4260 // Case 1: Quiet time. (QUIET)
4261#if DEBUG_GESTURES
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004262 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004263 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004264#endif
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004265 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4266 *outFinishPreviousGesture = true;
4267 }
Jeff Brownace13b12011-03-09 17:39:48 -08004268
4269 mPointerGesture.activeGestureId = -1;
4270 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004271 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004272
Jeff Brown65fd2512011-08-18 11:20:58 -07004273 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004274 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004275 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004276 // The pointer follows the active touch point.
4277 // Emit DOWN, MOVE, UP events at the pointer location.
4278 //
4279 // Only the active touch matters; other fingers are ignored. This policy helps
4280 // to handle the case where the user places a second finger on the touch pad
4281 // to apply the necessary force to depress an integrated button below the surface.
4282 // We don't want the second finger to be delivered to applications.
4283 //
4284 // For this to work well, we need to make sure to track the pointer that is really
4285 // active. If the user first puts one finger down to click then adds another
4286 // finger to drag then the active pointer should switch to the finger that is
4287 // being dragged.
4288#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004289 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004290 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004291#endif
4292 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004293 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004294 *outFinishPreviousGesture = true;
4295 mPointerGesture.activeGestureId = 0;
4296 }
4297
4298 // Switch pointers if needed.
4299 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004300 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d42011-06-01 12:33:19 -07004301 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004302 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004303 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004304 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d42011-06-01 12:33:19 -07004305 float vx, vy;
4306 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4307 float speed = hypotf(vx, vy);
4308 if (speed > bestSpeed) {
4309 bestId = id;
4310 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004311 }
Jeff Brown8d608662010-08-30 03:02:23 -07004312 }
Jeff Brown19c97d42011-06-01 12:33:19 -07004313 }
4314 if (bestId >= 0 && bestId != activeTouchId) {
4315 mPointerGesture.activeTouchId = activeTouchId = bestId;
4316 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004317#if DEBUG_GESTURES
Jeff Brown19c97d42011-06-01 12:33:19 -07004318 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4319 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004320#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004321 }
Jeff Brown19c97d42011-06-01 12:33:19 -07004322 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004323
Jeff Brown65fd2512011-08-18 11:20:58 -07004324 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004325 const RawPointerData::Pointer& currentPointer =
4326 mCurrentRawPointerData.pointerForId(activeTouchId);
4327 const RawPointerData::Pointer& lastPointer =
4328 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004329 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4330 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004331
Jeff Brownbe1aa822011-07-27 16:04:54 -07004332 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004333 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d42011-06-01 12:33:19 -07004334
4335 // Move the pointer using a relative motion.
4336 // When using spots, the click will occur at the position of the anchor
4337 // spot and all other spots will move there.
4338 mPointerController->move(deltaX, deltaY);
4339 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004340 mPointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004341 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004342
Jeff Brownace13b12011-03-09 17:39:48 -08004343 float x, y;
4344 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004345
Jeff Brown79ac9692011-04-19 21:20:10 -07004346 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004347 mPointerGesture.currentGestureIdBits.clear();
4348 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4349 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004350 mPointerGesture.currentGestureProperties[0].clear();
4351 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004352 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004353 mPointerGesture.currentGestureCoords[0].clear();
4354 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4355 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4356 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004357 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004358 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004359 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4360 *outFinishPreviousGesture = true;
4361 }
Jeff Brownace13b12011-03-09 17:39:48 -08004362
Jeff Brown79ac9692011-04-19 21:20:10 -07004363 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004364 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004365 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004366 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4367 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004368 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004369 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004370 float x, y;
4371 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004372 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4373 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004374#if DEBUG_GESTURES
4375 LOGD("Gestures: TAP");
4376#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004377
4378 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004379 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004380 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004381
Jeff Brownace13b12011-03-09 17:39:48 -08004382 mPointerGesture.activeGestureId = 0;
4383 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004384 mPointerGesture.currentGestureIdBits.clear();
4385 mPointerGesture.currentGestureIdBits.markBit(
4386 mPointerGesture.activeGestureId);
4387 mPointerGesture.currentGestureIdToIndex[
4388 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004389 mPointerGesture.currentGestureProperties[0].clear();
4390 mPointerGesture.currentGestureProperties[0].id =
4391 mPointerGesture.activeGestureId;
4392 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004393 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004394 mPointerGesture.currentGestureCoords[0].clear();
4395 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004396 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004397 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004398 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004399 mPointerGesture.currentGestureCoords[0].setAxisValue(
4400 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004401
Jeff Brownace13b12011-03-09 17:39:48 -08004402 tapped = true;
4403 } else {
4404#if DEBUG_GESTURES
4405 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004406 x - mPointerGesture.tapX,
4407 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004408#endif
4409 }
4410 } else {
4411#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004412 LOGD("Gestures: Not a TAP, %0.3fms since down",
4413 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004414#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004415 }
Jeff Brownace13b12011-03-09 17:39:48 -08004416 }
Jeff Brown2352b972011-04-12 22:39:53 -07004417
Jeff Brown65fd2512011-08-18 11:20:58 -07004418 mPointerVelocityControl.reset();
Jeff Brown19c97d42011-06-01 12:33:19 -07004419
Jeff Brownace13b12011-03-09 17:39:48 -08004420 if (!tapped) {
4421#if DEBUG_GESTURES
4422 LOGD("Gestures: NEUTRAL");
4423#endif
4424 mPointerGesture.activeGestureId = -1;
4425 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004426 mPointerGesture.currentGestureIdBits.clear();
4427 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004428 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004429 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004430 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004431 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4432 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004433 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004434
Jeff Brown79ac9692011-04-19 21:20:10 -07004435 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4436 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004437 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004438 float x, y;
4439 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004440 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4441 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004442 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4443 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004444#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004445 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4446 x - mPointerGesture.tapX,
4447 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004448#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004449 }
4450 } else {
4451#if DEBUG_GESTURES
4452 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4453 (when - mPointerGesture.tapUpTime) * 0.000001f);
4454#endif
4455 }
4456 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4457 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4458 }
Jeff Brownace13b12011-03-09 17:39:48 -08004459
Jeff Brown65fd2512011-08-18 11:20:58 -07004460 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004461 const RawPointerData::Pointer& currentPointer =
4462 mCurrentRawPointerData.pointerForId(activeTouchId);
4463 const RawPointerData::Pointer& lastPointer =
4464 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004465 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004466 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004467 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004468 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004469
Jeff Brownbe1aa822011-07-27 16:04:54 -07004470 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004471 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d42011-06-01 12:33:19 -07004472
Jeff Brown2352b972011-04-12 22:39:53 -07004473 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004474 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004475 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d42011-06-01 12:33:19 -07004476 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004477 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004478 }
4479
Jeff Brown79ac9692011-04-19 21:20:10 -07004480 bool down;
4481 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4482#if DEBUG_GESTURES
4483 LOGD("Gestures: TAP_DRAG");
4484#endif
4485 down = true;
4486 } else {
4487#if DEBUG_GESTURES
4488 LOGD("Gestures: HOVER");
4489#endif
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004490 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4491 *outFinishPreviousGesture = true;
4492 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004493 mPointerGesture.activeGestureId = 0;
4494 down = false;
4495 }
Jeff Brownace13b12011-03-09 17:39:48 -08004496
4497 float x, y;
4498 mPointerController->getPosition(&x, &y);
4499
Jeff Brownace13b12011-03-09 17:39:48 -08004500 mPointerGesture.currentGestureIdBits.clear();
4501 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4502 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004503 mPointerGesture.currentGestureProperties[0].clear();
4504 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4505 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004506 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004507 mPointerGesture.currentGestureCoords[0].clear();
4508 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4509 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004510 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4511 down ? 1.0f : 0.0f);
4512
Jeff Brown65fd2512011-08-18 11:20:58 -07004513 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004514 mPointerGesture.resetTap();
4515 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004516 mPointerGesture.tapX = x;
4517 mPointerGesture.tapY = y;
4518 }
Jeff Brownace13b12011-03-09 17:39:48 -08004519 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004520 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4521 // We need to provide feedback for each finger that goes down so we cannot wait
4522 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004523 //
Jeff Brown2352b972011-04-12 22:39:53 -07004524 // The ambiguous case is deciding what to do when there are two fingers down but they
4525 // have not moved enough to determine whether they are part of a drag or part of a
4526 // freeform gesture, or just a press or long-press at the pointer location.
4527 //
4528 // When there are two fingers we start with the PRESS hypothesis and we generate a
4529 // down at the pointer location.
4530 //
4531 // When the two fingers move enough or when additional fingers are added, we make
4532 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004533 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004534
Jeff Brown214eaf42011-05-26 19:17:02 -07004535 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004536 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004537 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004538 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4539 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004540 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004541 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d42011-06-01 12:33:19 -07004542 // Additional pointers have gone down but not yet settled.
4543 // Reset the gesture.
4544#if DEBUG_GESTURES
4545 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004546 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004547 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d42011-06-01 12:33:19 -07004548 * 0.000001f);
4549#endif
4550 *outCancelPreviousGesture = true;
4551 } else {
4552 // Continue previous gesture.
4553 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4554 }
4555
4556 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004557 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4558 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004559 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004560 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004561
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004562 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004563#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004564 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4565 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004566 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004567 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004568#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004569 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4570 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004571 &mPointerGesture.referenceTouchY);
4572 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4573 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004574 }
Jeff Brownace13b12011-03-09 17:39:48 -08004575
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004576 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004577 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004578 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4579 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004580 mPointerGesture.referenceDeltas[id].dx = 0;
4581 mPointerGesture.referenceDeltas[id].dy = 0;
4582 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004583 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004584
4585 // Add delta for all fingers and calculate a common movement delta.
4586 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004587 BitSet32 commonIdBits(mLastFingerIdBits.value
4588 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004589 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4590 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004591 uint32_t id = idBits.clearFirstMarkedBit();
4592 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4593 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004594 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4595 delta.dx += cpd.x - lpd.x;
4596 delta.dy += cpd.y - lpd.y;
4597
4598 if (first) {
4599 commonDeltaX = delta.dx;
4600 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004601 } else {
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004602 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4603 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4604 }
4605 }
Jeff Brownace13b12011-03-09 17:39:48 -08004606
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004607 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4608 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4609 float dist[MAX_POINTER_ID + 1];
4610 int32_t distOverThreshold = 0;
4611 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004612 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004613 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004614 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4615 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004616 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004617 distOverThreshold += 1;
4618 }
4619 }
4620
4621 // Only transition when at least two pointers have moved further than
4622 // the minimum distance threshold.
4623 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004624 if (currentFingerCount > 2) {
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004625 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004626#if DEBUG_GESTURES
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004627 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004628 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004629#endif
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004630 *outCancelPreviousGesture = true;
4631 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4632 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004633 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004634 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004635 uint32_t id1 = idBits.clearFirstMarkedBit();
4636 uint32_t id2 = idBits.firstMarkedBit();
4637 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4638 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4639 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4640 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4641 // There are two pointers but they are too far apart for a SWIPE,
4642 // switch to FREEFORM.
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004643#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004644 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4645 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004646#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004647 *outCancelPreviousGesture = true;
4648 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4649 } else {
4650 // There are two pointers. Wait for both pointers to start moving
4651 // before deciding whether this is a SWIPE or FREEFORM gesture.
4652 float dist1 = dist[id1];
4653 float dist2 = dist[id2];
4654 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4655 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4656 // Calculate the dot product of the displacement vectors.
4657 // When the vectors are oriented in approximately the same direction,
4658 // the angle betweeen them is near zero and the cosine of the angle
4659 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4660 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4661 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004662 float dx1 = delta1.dx * mPointerXZoomScale;
4663 float dy1 = delta1.dy * mPointerYZoomScale;
4664 float dx2 = delta2.dx * mPointerXZoomScale;
4665 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004666 float dot = dx1 * dx2 + dy1 * dy2;
4667 float cosine = dot / (dist1 * dist2); // denominator always > 0
4668 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4669 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004670#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004671 LOGD("Gestures: PRESS transitioned to SWIPE, "
4672 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4673 "cosine %0.3f >= %0.3f",
4674 dist1, mConfig.pointerGestureMultitouchMinDistance,
4675 dist2, mConfig.pointerGestureMultitouchMinDistance,
4676 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004677#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004678 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4679 } else {
4680 // Pointers are moving in different directions. Switch to FREEFORM.
4681#if DEBUG_GESTURES
4682 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4683 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4684 "cosine %0.3f < %0.3f",
4685 dist1, mConfig.pointerGestureMultitouchMinDistance,
4686 dist2, mConfig.pointerGestureMultitouchMinDistance,
4687 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4688#endif
4689 *outCancelPreviousGesture = true;
4690 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4691 }
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004692 }
Jeff Brownace13b12011-03-09 17:39:48 -08004693 }
4694 }
Jeff Brownace13b12011-03-09 17:39:48 -08004695 }
4696 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004697 // Switch from SWIPE to FREEFORM if additional pointers go down.
4698 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004699 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004700#if DEBUG_GESTURES
4701 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004702 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004703#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004704 *outCancelPreviousGesture = true;
4705 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004706 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004707 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004708
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004709 // Move the reference points based on the overall group motion of the fingers
4710 // except in PRESS mode while waiting for a transition to occur.
4711 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4712 && (commonDeltaX || commonDeltaY)) {
4713 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004714 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004715 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004716 delta.dx = 0;
4717 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004718 }
4719
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004720 mPointerGesture.referenceTouchX += commonDeltaX;
4721 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004722
Jeff Brown65fd2512011-08-18 11:20:58 -07004723 commonDeltaX *= mPointerXMovementScale;
4724 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004725
Jeff Brownbe1aa822011-07-27 16:04:54 -07004726 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004727 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004728
Jeff Brownbb3fcba2011-06-06 19:23:05 -07004729 mPointerGesture.referenceGestureX += commonDeltaX;
4730 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004731 }
4732
4733 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004734 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4735 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4736 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004737#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004738 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004739 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004740 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004741#endif
4742 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4743
4744 mPointerGesture.currentGestureIdBits.clear();
4745 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4746 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004747 mPointerGesture.currentGestureProperties[0].clear();
4748 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4749 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004750 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004751 mPointerGesture.currentGestureCoords[0].clear();
4752 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4753 mPointerGesture.referenceGestureX);
4754 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4755 mPointerGesture.referenceGestureY);
4756 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004757 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4758 // FREEFORM mode.
4759#if DEBUG_GESTURES
4760 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4761 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004762 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004763#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004764 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004765
Jeff Brownace13b12011-03-09 17:39:48 -08004766 mPointerGesture.currentGestureIdBits.clear();
4767
4768 BitSet32 mappedTouchIdBits;
4769 BitSet32 usedGestureIdBits;
4770 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4771 // Initially, assign the active gesture id to the active touch point
4772 // if there is one. No other touch id bits are mapped yet.
4773 if (!*outCancelPreviousGesture) {
4774 mappedTouchIdBits.markBit(activeTouchId);
4775 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4776 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4777 mPointerGesture.activeGestureId;
4778 } else {
4779 mPointerGesture.activeGestureId = -1;
4780 }
4781 } else {
4782 // Otherwise, assume we mapped all touches from the previous frame.
4783 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004784 mappedTouchIdBits.value = mLastFingerIdBits.value
4785 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004786 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4787
4788 // Check whether we need to choose a new active gesture id because the
4789 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004790 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4791 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004792 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004793 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004794 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4795 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4796 mPointerGesture.activeGestureId = -1;
4797 break;
4798 }
4799 }
4800 }
4801
4802#if DEBUG_GESTURES
4803 LOGD("Gestures: FREEFORM follow up "
4804 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4805 "activeGestureId=%d",
4806 mappedTouchIdBits.value, usedGestureIdBits.value,
4807 mPointerGesture.activeGestureId);
4808#endif
4809
Jeff Brown65fd2512011-08-18 11:20:58 -07004810 BitSet32 idBits(mCurrentFingerIdBits);
4811 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004812 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004813 uint32_t gestureId;
4814 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004815 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004816 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4817#if DEBUG_GESTURES
4818 LOGD("Gestures: FREEFORM "
4819 "new mapping for touch id %d -> gesture id %d",
4820 touchId, gestureId);
4821#endif
4822 } else {
4823 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4824#if DEBUG_GESTURES
4825 LOGD("Gestures: FREEFORM "
4826 "existing mapping for touch id %d -> gesture id %d",
4827 touchId, gestureId);
4828#endif
4829 }
4830 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4831 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4832
Jeff Brownbe1aa822011-07-27 16:04:54 -07004833 const RawPointerData::Pointer& pointer =
4834 mCurrentRawPointerData.pointerForId(touchId);
4835 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004836 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004837 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004838 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004839 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004840
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004841 mPointerGesture.currentGestureProperties[i].clear();
4842 mPointerGesture.currentGestureProperties[i].id = gestureId;
4843 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004844 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004845 mPointerGesture.currentGestureCoords[i].clear();
4846 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004847 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004848 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004849 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004850 mPointerGesture.currentGestureCoords[i].setAxisValue(
4851 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4852 }
4853
4854 if (mPointerGesture.activeGestureId < 0) {
4855 mPointerGesture.activeGestureId =
4856 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4857#if DEBUG_GESTURES
4858 LOGD("Gestures: FREEFORM new "
4859 "activeGestureId=%d", mPointerGesture.activeGestureId);
4860#endif
4861 }
Jeff Brown2352b972011-04-12 22:39:53 -07004862 }
Jeff Brownace13b12011-03-09 17:39:48 -08004863 }
4864
Jeff Brownbe1aa822011-07-27 16:04:54 -07004865 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004866
Jeff Brownace13b12011-03-09 17:39:48 -08004867#if DEBUG_GESTURES
4868 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004869 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4870 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004871 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004872 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4873 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004874 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004875 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004876 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004877 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004878 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004879 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4880 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4881 id, index, properties.toolType,
4882 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004883 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4884 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4885 }
4886 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004887 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004888 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004889 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004890 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004891 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4892 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4893 id, index, properties.toolType,
4894 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004895 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4896 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4897 }
4898#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004899 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004900}
4901
Jeff Brown65fd2512011-08-18 11:20:58 -07004902void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4903 mPointerSimple.currentCoords.clear();
4904 mPointerSimple.currentProperties.clear();
4905
4906 bool down, hovering;
4907 if (!mCurrentStylusIdBits.isEmpty()) {
4908 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4909 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4910 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4911 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4912 mPointerController->setPosition(x, y);
4913
4914 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4915 down = !hovering;
4916
4917 mPointerController->getPosition(&x, &y);
4918 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4919 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4920 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4921 mPointerSimple.currentProperties.id = 0;
4922 mPointerSimple.currentProperties.toolType =
4923 mCurrentCookedPointerData.pointerProperties[index].toolType;
4924 } else {
4925 down = false;
4926 hovering = false;
4927 }
4928
4929 dispatchPointerSimple(when, policyFlags, down, hovering);
4930}
4931
4932void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
4933 abortPointerSimple(when, policyFlags);
4934}
4935
4936void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
4937 mPointerSimple.currentCoords.clear();
4938 mPointerSimple.currentProperties.clear();
4939
4940 bool down, hovering;
4941 if (!mCurrentMouseIdBits.isEmpty()) {
4942 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
4943 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
4944 if (mLastMouseIdBits.hasBit(id)) {
4945 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
4946 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
4947 - mLastRawPointerData.pointers[lastIndex].x)
4948 * mPointerXMovementScale;
4949 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
4950 - mLastRawPointerData.pointers[lastIndex].y)
4951 * mPointerYMovementScale;
4952
4953 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
4954 mPointerVelocityControl.move(when, &deltaX, &deltaY);
4955
4956 mPointerController->move(deltaX, deltaY);
4957 } else {
4958 mPointerVelocityControl.reset();
4959 }
4960
4961 down = isPointerDown(mCurrentButtonState);
4962 hovering = !down;
4963
4964 float x, y;
4965 mPointerController->getPosition(&x, &y);
4966 mPointerSimple.currentCoords.copyFrom(
4967 mCurrentCookedPointerData.pointerCoords[currentIndex]);
4968 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4969 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4970 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4971 hovering ? 0.0f : 1.0f);
4972 mPointerSimple.currentProperties.id = 0;
4973 mPointerSimple.currentProperties.toolType =
4974 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
4975 } else {
4976 mPointerVelocityControl.reset();
4977
4978 down = false;
4979 hovering = false;
4980 }
4981
4982 dispatchPointerSimple(when, policyFlags, down, hovering);
4983}
4984
4985void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
4986 abortPointerSimple(when, policyFlags);
4987
4988 mPointerVelocityControl.reset();
4989}
4990
4991void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
4992 bool down, bool hovering) {
4993 int32_t metaState = getContext()->getGlobalMetaState();
4994
4995 if (mPointerController != NULL) {
4996 if (down || hovering) {
4997 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4998 mPointerController->clearSpots();
4999 mPointerController->setButtonState(mCurrentButtonState);
5000 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5001 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5002 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5003 }
5004 }
5005
5006 if (mPointerSimple.down && !down) {
5007 mPointerSimple.down = false;
5008
5009 // Send up.
5010 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5011 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5012 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5013 mOrientedXPrecision, mOrientedYPrecision,
5014 mPointerSimple.downTime);
5015 getListener()->notifyMotion(&args);
5016 }
5017
5018 if (mPointerSimple.hovering && !hovering) {
5019 mPointerSimple.hovering = false;
5020
5021 // Send hover exit.
5022 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5023 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5024 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5025 mOrientedXPrecision, mOrientedYPrecision,
5026 mPointerSimple.downTime);
5027 getListener()->notifyMotion(&args);
5028 }
5029
5030 if (down) {
5031 if (!mPointerSimple.down) {
5032 mPointerSimple.down = true;
5033 mPointerSimple.downTime = when;
5034
5035 // Send down.
5036 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5037 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5038 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5039 mOrientedXPrecision, mOrientedYPrecision,
5040 mPointerSimple.downTime);
5041 getListener()->notifyMotion(&args);
5042 }
5043
5044 // Send move.
5045 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5046 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5047 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5048 mOrientedXPrecision, mOrientedYPrecision,
5049 mPointerSimple.downTime);
5050 getListener()->notifyMotion(&args);
5051 }
5052
5053 if (hovering) {
5054 if (!mPointerSimple.hovering) {
5055 mPointerSimple.hovering = true;
5056
5057 // Send hover enter.
5058 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5059 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5060 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5061 mOrientedXPrecision, mOrientedYPrecision,
5062 mPointerSimple.downTime);
5063 getListener()->notifyMotion(&args);
5064 }
5065
5066 // Send hover move.
5067 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5068 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5069 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5070 mOrientedXPrecision, mOrientedYPrecision,
5071 mPointerSimple.downTime);
5072 getListener()->notifyMotion(&args);
5073 }
5074
5075 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5076 float vscroll = mCurrentRawVScroll;
5077 float hscroll = mCurrentRawHScroll;
5078 mWheelYVelocityControl.move(when, NULL, &vscroll);
5079 mWheelXVelocityControl.move(when, &hscroll, NULL);
5080
5081 // Send scroll.
5082 PointerCoords pointerCoords;
5083 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5084 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5085 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5086
5087 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5088 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5089 1, &mPointerSimple.currentProperties, &pointerCoords,
5090 mOrientedXPrecision, mOrientedYPrecision,
5091 mPointerSimple.downTime);
5092 getListener()->notifyMotion(&args);
5093 }
5094
5095 // Save state.
5096 if (down || hovering) {
5097 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5098 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5099 } else {
5100 mPointerSimple.reset();
5101 }
5102}
5103
5104void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5105 mPointerSimple.currentCoords.clear();
5106 mPointerSimple.currentProperties.clear();
5107
5108 dispatchPointerSimple(when, policyFlags, false, false);
5109}
5110
Jeff Brownace13b12011-03-09 17:39:48 -08005111void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005112 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5113 const PointerProperties* properties, const PointerCoords* coords,
5114 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005115 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5116 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005117 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005118 uint32_t pointerCount = 0;
5119 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005120 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005121 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005122 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005123 pointerCoords[pointerCount].copyFrom(coords[index]);
5124
5125 if (changedId >= 0 && id == uint32_t(changedId)) {
5126 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5127 }
5128
5129 pointerCount += 1;
5130 }
5131
Jeff Brownb6110c22011-04-01 16:15:13 -07005132 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005133
5134 if (changedId >= 0 && pointerCount == 1) {
5135 // Replace initial down and final up action.
5136 // We can compare the action without masking off the changed pointer index
5137 // because we know the index is 0.
5138 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5139 action = AMOTION_EVENT_ACTION_DOWN;
5140 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5141 action = AMOTION_EVENT_ACTION_UP;
5142 } else {
5143 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07005144 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005145 }
5146 }
5147
Jeff Brownbe1aa822011-07-27 16:04:54 -07005148 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005149 action, flags, metaState, buttonState, edgeFlags,
5150 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005151 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005152}
5153
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005154bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005155 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005156 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5157 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005158 bool changed = false;
5159 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005160 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005161 uint32_t inIndex = inIdToIndex[id];
5162 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005163
5164 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005165 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005166 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005167 PointerCoords& curOutCoords = outCoords[outIndex];
5168
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005169 if (curInProperties != curOutProperties) {
5170 curOutProperties.copyFrom(curInProperties);
5171 changed = true;
5172 }
5173
Jeff Brownace13b12011-03-09 17:39:48 -08005174 if (curInCoords != curOutCoords) {
5175 curOutCoords.copyFrom(curInCoords);
5176 changed = true;
5177 }
5178 }
5179 return changed;
5180}
5181
5182void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005183 if (mPointerController != NULL) {
5184 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5185 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005186}
5187
Jeff Brownbe1aa822011-07-27 16:04:54 -07005188bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5189 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5190 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005191}
5192
Jeff Brownbe1aa822011-07-27 16:04:54 -07005193const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005194 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005195 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005196 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005197 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005198
5199#if DEBUG_VIRTUAL_KEYS
5200 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
5201 "left=%d, top=%d, right=%d, bottom=%d",
5202 x, y,
5203 virtualKey.keyCode, virtualKey.scanCode,
5204 virtualKey.hitLeft, virtualKey.hitTop,
5205 virtualKey.hitRight, virtualKey.hitBottom);
5206#endif
5207
5208 if (virtualKey.isHit(x, y)) {
5209 return & virtualKey;
5210 }
5211 }
5212
5213 return NULL;
5214}
5215
Jeff Brownbe1aa822011-07-27 16:04:54 -07005216void TouchInputMapper::assignPointerIds() {
5217 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5218 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5219
5220 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005221
5222 if (currentPointerCount == 0) {
5223 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005224 return;
5225 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005226
Jeff Brownbe1aa822011-07-27 16:04:54 -07005227 if (lastPointerCount == 0) {
5228 // All pointers are new.
5229 for (uint32_t i = 0; i < currentPointerCount; i++) {
5230 uint32_t id = i;
5231 mCurrentRawPointerData.pointers[i].id = id;
5232 mCurrentRawPointerData.idToIndex[id] = i;
5233 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5234 }
5235 return;
5236 }
5237
5238 if (currentPointerCount == 1 && lastPointerCount == 1
5239 && mCurrentRawPointerData.pointers[0].toolType
5240 == mLastRawPointerData.pointers[0].toolType) {
5241 // Only one pointer and no change in count so it must have the same id as before.
5242 uint32_t id = mLastRawPointerData.pointers[0].id;
5243 mCurrentRawPointerData.pointers[0].id = id;
5244 mCurrentRawPointerData.idToIndex[id] = 0;
5245 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5246 return;
5247 }
5248
5249 // General case.
5250 // We build a heap of squared euclidean distances between current and last pointers
5251 // associated with the current and last pointer indices. Then, we find the best
5252 // match (by distance) for each current pointer.
5253 // The pointers must have the same tool type but it is possible for them to
5254 // transition from hovering to touching or vice-versa while retaining the same id.
5255 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5256
5257 uint32_t heapSize = 0;
5258 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5259 currentPointerIndex++) {
5260 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5261 lastPointerIndex++) {
5262 const RawPointerData::Pointer& currentPointer =
5263 mCurrentRawPointerData.pointers[currentPointerIndex];
5264 const RawPointerData::Pointer& lastPointer =
5265 mLastRawPointerData.pointers[lastPointerIndex];
5266 if (currentPointer.toolType == lastPointer.toolType) {
5267 int64_t deltaX = currentPointer.x - lastPointer.x;
5268 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005269
5270 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5271
5272 // Insert new element into the heap (sift up).
5273 heap[heapSize].currentPointerIndex = currentPointerIndex;
5274 heap[heapSize].lastPointerIndex = lastPointerIndex;
5275 heap[heapSize].distance = distance;
5276 heapSize += 1;
5277 }
5278 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005279 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005280
Jeff Brownbe1aa822011-07-27 16:04:54 -07005281 // Heapify
5282 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5283 startIndex -= 1;
5284 for (uint32_t parentIndex = startIndex; ;) {
5285 uint32_t childIndex = parentIndex * 2 + 1;
5286 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005287 break;
5288 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005289
5290 if (childIndex + 1 < heapSize
5291 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5292 childIndex += 1;
5293 }
5294
5295 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5296 break;
5297 }
5298
5299 swap(heap[parentIndex], heap[childIndex]);
5300 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005301 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005302 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005303
5304#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07005305 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
5306 for (size_t i = 0; i < heapSize; i++) {
5307 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5308 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5309 heap[i].distance);
5310 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005311#endif
5312
Jeff Brownbe1aa822011-07-27 16:04:54 -07005313 // Pull matches out by increasing order of distance.
5314 // To avoid reassigning pointers that have already been matched, the loop keeps track
5315 // of which last and current pointers have been matched using the matchedXXXBits variables.
5316 // It also tracks the used pointer id bits.
5317 BitSet32 matchedLastBits(0);
5318 BitSet32 matchedCurrentBits(0);
5319 BitSet32 usedIdBits(0);
5320 bool first = true;
5321 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5322 while (heapSize > 0) {
5323 if (first) {
5324 // The first time through the loop, we just consume the root element of
5325 // the heap (the one with smallest distance).
5326 first = false;
5327 } else {
5328 // Previous iterations consumed the root element of the heap.
5329 // Pop root element off of the heap (sift down).
5330 heap[0] = heap[heapSize];
5331 for (uint32_t parentIndex = 0; ;) {
5332 uint32_t childIndex = parentIndex * 2 + 1;
5333 if (childIndex >= heapSize) {
5334 break;
5335 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005336
Jeff Brownbe1aa822011-07-27 16:04:54 -07005337 if (childIndex + 1 < heapSize
5338 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5339 childIndex += 1;
5340 }
5341
5342 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5343 break;
5344 }
5345
5346 swap(heap[parentIndex], heap[childIndex]);
5347 parentIndex = childIndex;
5348 }
5349
5350#if DEBUG_POINTER_ASSIGNMENT
5351 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
5352 for (size_t i = 0; i < heapSize; i++) {
5353 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5354 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5355 heap[i].distance);
5356 }
5357#endif
5358 }
5359
5360 heapSize -= 1;
5361
5362 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5363 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5364
5365 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5366 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5367
5368 matchedCurrentBits.markBit(currentPointerIndex);
5369 matchedLastBits.markBit(lastPointerIndex);
5370
5371 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5372 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5373 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5374 mCurrentRawPointerData.markIdBit(id,
5375 mCurrentRawPointerData.isHovering(currentPointerIndex));
5376 usedIdBits.markBit(id);
5377
5378#if DEBUG_POINTER_ASSIGNMENT
5379 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
5380 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5381#endif
5382 break;
5383 }
5384 }
5385
5386 // Assign fresh ids to pointers that were not matched in the process.
5387 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5388 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5389 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5390
5391 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5392 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5393 mCurrentRawPointerData.markIdBit(id,
5394 mCurrentRawPointerData.isHovering(currentPointerIndex));
5395
5396#if DEBUG_POINTER_ASSIGNMENT
5397 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
5398 currentPointerIndex, id);
5399#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005400 }
5401}
5402
Jeff Brown6d0fec22010-07-23 21:28:06 -07005403int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005404 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5405 return AKEY_STATE_VIRTUAL;
5406 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005407
Jeff Brownbe1aa822011-07-27 16:04:54 -07005408 size_t numVirtualKeys = mVirtualKeys.size();
5409 for (size_t i = 0; i < numVirtualKeys; i++) {
5410 const VirtualKey& virtualKey = mVirtualKeys[i];
5411 if (virtualKey.keyCode == keyCode) {
5412 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005413 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005414 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005415
5416 return AKEY_STATE_UNKNOWN;
5417}
5418
5419int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005420 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5421 return AKEY_STATE_VIRTUAL;
5422 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005423
Jeff Brownbe1aa822011-07-27 16:04:54 -07005424 size_t numVirtualKeys = mVirtualKeys.size();
5425 for (size_t i = 0; i < numVirtualKeys; i++) {
5426 const VirtualKey& virtualKey = mVirtualKeys[i];
5427 if (virtualKey.scanCode == scanCode) {
5428 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005429 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005430 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005431
5432 return AKEY_STATE_UNKNOWN;
5433}
5434
5435bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5436 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005437 size_t numVirtualKeys = mVirtualKeys.size();
5438 for (size_t i = 0; i < numVirtualKeys; i++) {
5439 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005440
Jeff Brownbe1aa822011-07-27 16:04:54 -07005441 for (size_t i = 0; i < numCodes; i++) {
5442 if (virtualKey.keyCode == keyCodes[i]) {
5443 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005444 }
5445 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005446 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005447
5448 return true;
5449}
5450
5451
5452// --- SingleTouchInputMapper ---
5453
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005454SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5455 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005456}
5457
5458SingleTouchInputMapper::~SingleTouchInputMapper() {
5459}
5460
Jeff Brown65fd2512011-08-18 11:20:58 -07005461void SingleTouchInputMapper::reset(nsecs_t when) {
5462 mSingleTouchMotionAccumulator.reset(getDevice());
5463
5464 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005465}
5466
Jeff Brown6d0fec22010-07-23 21:28:06 -07005467void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005468 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005469
Jeff Brown65fd2512011-08-18 11:20:58 -07005470 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005471}
5472
Jeff Brown65fd2512011-08-18 11:20:58 -07005473void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005474 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005475 mCurrentRawPointerData.pointerCount = 1;
5476 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005477
Jeff Brown65fd2512011-08-18 11:20:58 -07005478 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5479 && (mTouchButtonAccumulator.isHovering()
5480 || (mRawPointerAxes.pressure.valid
5481 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005482 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005483
Jeff Brownbe1aa822011-07-27 16:04:54 -07005484 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005485 outPointer.id = 0;
5486 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5487 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5488 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5489 outPointer.touchMajor = 0;
5490 outPointer.touchMinor = 0;
5491 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5492 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5493 outPointer.orientation = 0;
5494 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005495 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5496 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005497 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5498 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5499 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5500 }
5501 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005502 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005503}
5504
Jeff Brownbe1aa822011-07-27 16:04:54 -07005505void SingleTouchInputMapper::configureRawPointerAxes() {
5506 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005507
Jeff Brownbe1aa822011-07-27 16:04:54 -07005508 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5509 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5510 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5511 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5512 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005513 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5514 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005515}
5516
5517
5518// --- MultiTouchInputMapper ---
5519
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005520MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005521 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005522}
5523
5524MultiTouchInputMapper::~MultiTouchInputMapper() {
5525}
5526
Jeff Brown65fd2512011-08-18 11:20:58 -07005527void MultiTouchInputMapper::reset(nsecs_t when) {
5528 mMultiTouchMotionAccumulator.reset(getDevice());
5529
Jeff Brown6894a292011-07-01 17:59:27 -07005530 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005531
Jeff Brown65fd2512011-08-18 11:20:58 -07005532 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005533}
5534
5535void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005536 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005537
Jeff Brown65fd2512011-08-18 11:20:58 -07005538 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005539}
5540
Jeff Brown65fd2512011-08-18 11:20:58 -07005541void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005542 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005543 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005544 BitSet32 newPointerIdBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005545
Jeff Brown80fd47c2011-05-24 01:07:44 -07005546 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005547 const MultiTouchMotionAccumulator::Slot* inSlot =
5548 mMultiTouchMotionAccumulator.getSlot(inIndex);
5549 if (!inSlot->isInUse()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07005550 continue;
5551 }
5552
Jeff Brown80fd47c2011-05-24 01:07:44 -07005553 if (outCount >= MAX_POINTERS) {
5554#if DEBUG_POINTERS
5555 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5556 "ignoring the rest.",
5557 getDeviceName().string(), MAX_POINTERS);
5558#endif
5559 break; // too many fingers!
5560 }
5561
Jeff Brownbe1aa822011-07-27 16:04:54 -07005562 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005563 outPointer.x = inSlot->getX();
5564 outPointer.y = inSlot->getY();
5565 outPointer.pressure = inSlot->getPressure();
5566 outPointer.touchMajor = inSlot->getTouchMajor();
5567 outPointer.touchMinor = inSlot->getTouchMinor();
5568 outPointer.toolMajor = inSlot->getToolMajor();
5569 outPointer.toolMinor = inSlot->getToolMinor();
5570 outPointer.orientation = inSlot->getOrientation();
5571 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005572 outPointer.tiltX = 0;
5573 outPointer.tiltY = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005574
Jeff Brown49754db2011-07-01 17:37:58 -07005575 outPointer.toolType = inSlot->getToolType();
5576 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5577 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5578 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5579 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5580 }
Jeff Brown8d608662010-08-30 03:02:23 -07005581 }
5582
Jeff Brown65fd2512011-08-18 11:20:58 -07005583 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5584 && (mTouchButtonAccumulator.isHovering()
5585 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005586 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005587
Jeff Brown8d608662010-08-30 03:02:23 -07005588 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005589 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005590 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005591 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005592 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005593 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005594 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005595 if (mPointerTrackingIdMap[n] == trackingId) {
5596 id = n;
5597 }
5598 }
5599
5600 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005601 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005602 mPointerTrackingIdMap[id] = trackingId;
5603 }
5604 }
5605 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005606 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005607 mCurrentRawPointerData.clearIdBits();
5608 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005609 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005610 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005611 mCurrentRawPointerData.idToIndex[id] = outCount;
5612 mCurrentRawPointerData.markIdBit(id, isHovering);
5613 newPointerIdBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005614 }
5615 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005616
Jeff Brown6d0fec22010-07-23 21:28:06 -07005617 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005618 }
5619
Jeff Brownbe1aa822011-07-27 16:04:54 -07005620 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005621 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005622
Jeff Brown65fd2512011-08-18 11:20:58 -07005623 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005624}
5625
Jeff Brownbe1aa822011-07-27 16:04:54 -07005626void MultiTouchInputMapper::configureRawPointerAxes() {
5627 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005628
Jeff Brownbe1aa822011-07-27 16:04:54 -07005629 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5630 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5631 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5632 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5633 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5634 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5635 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5636 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5637 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5638 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5639 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005640
Jeff Brownbe1aa822011-07-27 16:04:54 -07005641 if (mRawPointerAxes.trackingId.valid
5642 && mRawPointerAxes.slot.valid
5643 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5644 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005645 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005646 LOGW("MultiTouch Device %s reported %d slots but the framework "
5647 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005648 getDeviceName().string(), slotCount, MAX_SLOTS);
5649 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005650 }
Jeff Brown49754db2011-07-01 17:37:58 -07005651 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005652 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005653 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005654 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005655}
5656
Jeff Brown46b9ac02010-04-22 18:58:52 -07005657
Jeff Browncb1404e2011-01-15 18:14:15 -08005658// --- JoystickInputMapper ---
5659
5660JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5661 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005662}
5663
5664JoystickInputMapper::~JoystickInputMapper() {
5665}
5666
5667uint32_t JoystickInputMapper::getSources() {
5668 return AINPUT_SOURCE_JOYSTICK;
5669}
5670
5671void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5672 InputMapper::populateDeviceInfo(info);
5673
Jeff Brown6f2fba42011-02-19 01:08:02 -08005674 for (size_t i = 0; i < mAxes.size(); i++) {
5675 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005676 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5677 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005678 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005679 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5680 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005681 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005682 }
5683}
5684
5685void JoystickInputMapper::dump(String8& dump) {
5686 dump.append(INDENT2 "Joystick Input Mapper:\n");
5687
Jeff Brown6f2fba42011-02-19 01:08:02 -08005688 dump.append(INDENT3 "Axes:\n");
5689 size_t numAxes = mAxes.size();
5690 for (size_t i = 0; i < numAxes; i++) {
5691 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005692 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005693 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005694 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005695 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005696 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005697 }
Jeff Brown85297452011-03-04 13:07:49 -08005698 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5699 label = getAxisLabel(axis.axisInfo.highAxis);
5700 if (label) {
5701 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5702 } else {
5703 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5704 axis.axisInfo.splitValue);
5705 }
5706 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5707 dump.append(" (invert)");
5708 }
5709
5710 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5711 axis.min, axis.max, axis.flat, axis.fuzz);
5712 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5713 "highScale=%0.5f, highOffset=%0.5f\n",
5714 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005715 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5716 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005717 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005718 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005719 }
5720}
5721
Jeff Brown65fd2512011-08-18 11:20:58 -07005722void JoystickInputMapper::configure(nsecs_t when,
5723 const InputReaderConfiguration* config, uint32_t changes) {
5724 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005725
Jeff Brown474dcb52011-06-14 20:22:50 -07005726 if (!changes) { // first time only
5727 // Collect all axes.
5728 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5729 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005730 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005731 if (rawAxisInfo.valid) {
5732 // Map axis.
5733 AxisInfo axisInfo;
5734 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5735 if (!explicitlyMapped) {
5736 // Axis is not explicitly mapped, will choose a generic axis later.
5737 axisInfo.mode = AxisInfo::MODE_NORMAL;
5738 axisInfo.axis = -1;
5739 }
5740
5741 // Apply flat override.
5742 int32_t rawFlat = axisInfo.flatOverride < 0
5743 ? rawAxisInfo.flat : axisInfo.flatOverride;
5744
5745 // Calculate scaling factors and limits.
5746 Axis axis;
5747 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5748 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5749 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5750 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5751 scale, 0.0f, highScale, 0.0f,
5752 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5753 } else if (isCenteredAxis(axisInfo.axis)) {
5754 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5755 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5756 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5757 scale, offset, scale, offset,
5758 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5759 } else {
5760 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5761 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5762 scale, 0.0f, scale, 0.0f,
5763 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5764 }
5765
5766 // To eliminate noise while the joystick is at rest, filter out small variations
5767 // in axis values up front.
5768 axis.filter = axis.flat * 0.25f;
5769
5770 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005771 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005772 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005773
Jeff Brown474dcb52011-06-14 20:22:50 -07005774 // If there are too many axes, start dropping them.
5775 // Prefer to keep explicitly mapped axes.
5776 if (mAxes.size() > PointerCoords::MAX_AXES) {
5777 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5778 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5779 pruneAxes(true);
5780 pruneAxes(false);
5781 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005782
Jeff Brown474dcb52011-06-14 20:22:50 -07005783 // Assign generic axis ids to remaining axes.
5784 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5785 size_t numAxes = mAxes.size();
5786 for (size_t i = 0; i < numAxes; i++) {
5787 Axis& axis = mAxes.editValueAt(i);
5788 if (axis.axisInfo.axis < 0) {
5789 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5790 && haveAxis(nextGenericAxisId)) {
5791 nextGenericAxisId += 1;
5792 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005793
Jeff Brown474dcb52011-06-14 20:22:50 -07005794 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5795 axis.axisInfo.axis = nextGenericAxisId;
5796 nextGenericAxisId += 1;
5797 } else {
5798 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5799 "have already been assigned to other axes.",
5800 getDeviceName().string(), mAxes.keyAt(i));
5801 mAxes.removeItemsAt(i--);
5802 numAxes -= 1;
5803 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005804 }
5805 }
5806 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005807}
5808
Jeff Brown85297452011-03-04 13:07:49 -08005809bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005810 size_t numAxes = mAxes.size();
5811 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005812 const Axis& axis = mAxes.valueAt(i);
5813 if (axis.axisInfo.axis == axisId
5814 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5815 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005816 return true;
5817 }
5818 }
5819 return false;
5820}
Jeff Browncb1404e2011-01-15 18:14:15 -08005821
Jeff Brown6f2fba42011-02-19 01:08:02 -08005822void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5823 size_t i = mAxes.size();
5824 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5825 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5826 continue;
5827 }
5828 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5829 getDeviceName().string(), mAxes.keyAt(i));
5830 mAxes.removeItemsAt(i);
5831 }
5832}
5833
5834bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5835 switch (axis) {
5836 case AMOTION_EVENT_AXIS_X:
5837 case AMOTION_EVENT_AXIS_Y:
5838 case AMOTION_EVENT_AXIS_Z:
5839 case AMOTION_EVENT_AXIS_RX:
5840 case AMOTION_EVENT_AXIS_RY:
5841 case AMOTION_EVENT_AXIS_RZ:
5842 case AMOTION_EVENT_AXIS_HAT_X:
5843 case AMOTION_EVENT_AXIS_HAT_Y:
5844 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005845 case AMOTION_EVENT_AXIS_RUDDER:
5846 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005847 return true;
5848 default:
5849 return false;
5850 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005851}
5852
Jeff Brown65fd2512011-08-18 11:20:58 -07005853void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005854 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005855 size_t numAxes = mAxes.size();
5856 for (size_t i = 0; i < numAxes; i++) {
5857 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005858 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005859 }
5860
Jeff Brown65fd2512011-08-18 11:20:58 -07005861 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005862}
5863
5864void JoystickInputMapper::process(const RawEvent* rawEvent) {
5865 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005866 case EV_ABS: {
5867 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5868 if (index >= 0) {
5869 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005870 float newValue, highNewValue;
5871 switch (axis.axisInfo.mode) {
5872 case AxisInfo::MODE_INVERT:
5873 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5874 * axis.scale + axis.offset;
5875 highNewValue = 0.0f;
5876 break;
5877 case AxisInfo::MODE_SPLIT:
5878 if (rawEvent->value < axis.axisInfo.splitValue) {
5879 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5880 * axis.scale + axis.offset;
5881 highNewValue = 0.0f;
5882 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5883 newValue = 0.0f;
5884 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5885 * axis.highScale + axis.highOffset;
5886 } else {
5887 newValue = 0.0f;
5888 highNewValue = 0.0f;
5889 }
5890 break;
5891 default:
5892 newValue = rawEvent->value * axis.scale + axis.offset;
5893 highNewValue = 0.0f;
5894 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005895 }
Jeff Brown85297452011-03-04 13:07:49 -08005896 axis.newValue = newValue;
5897 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005898 }
5899 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005900 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005901
5902 case EV_SYN:
5903 switch (rawEvent->scanCode) {
5904 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005905 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005906 break;
5907 }
5908 break;
5909 }
5910}
5911
Jeff Brown6f2fba42011-02-19 01:08:02 -08005912void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005913 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005914 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005915 }
5916
5917 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005918 int32_t buttonState = 0;
5919
5920 PointerProperties pointerProperties;
5921 pointerProperties.clear();
5922 pointerProperties.id = 0;
5923 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005924
Jeff Brown6f2fba42011-02-19 01:08:02 -08005925 PointerCoords pointerCoords;
5926 pointerCoords.clear();
5927
5928 size_t numAxes = mAxes.size();
5929 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005930 const Axis& axis = mAxes.valueAt(i);
5931 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5932 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5933 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5934 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005935 }
5936
Jeff Brown56194eb2011-03-02 19:23:13 -08005937 // Moving a joystick axis should not wake the devide because joysticks can
5938 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5939 // button will likely wake the device.
5940 // TODO: Use the input device configuration to control this behavior more finely.
5941 uint32_t policyFlags = 0;
5942
Jeff Brownbe1aa822011-07-27 16:04:54 -07005943 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005944 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5945 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005946 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005947}
5948
Jeff Brown85297452011-03-04 13:07:49 -08005949bool JoystickInputMapper::filterAxes(bool force) {
5950 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005951 size_t numAxes = mAxes.size();
5952 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005953 Axis& axis = mAxes.editValueAt(i);
5954 if (force || hasValueChangedSignificantly(axis.filter,
5955 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5956 axis.currentValue = axis.newValue;
5957 atLeastOneSignificantChange = true;
5958 }
5959 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5960 if (force || hasValueChangedSignificantly(axis.filter,
5961 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5962 axis.highCurrentValue = axis.highNewValue;
5963 atLeastOneSignificantChange = true;
5964 }
5965 }
5966 }
5967 return atLeastOneSignificantChange;
5968}
5969
5970bool JoystickInputMapper::hasValueChangedSignificantly(
5971 float filter, float newValue, float currentValue, float min, float max) {
5972 if (newValue != currentValue) {
5973 // Filter out small changes in value unless the value is converging on the axis
5974 // bounds or center point. This is intended to reduce the amount of information
5975 // sent to applications by particularly noisy joysticks (such as PS3).
5976 if (fabs(newValue - currentValue) > filter
5977 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5978 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5979 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5980 return true;
5981 }
5982 }
5983 return false;
5984}
5985
5986bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5987 float filter, float newValue, float currentValue, float thresholdValue) {
5988 float newDistance = fabs(newValue - thresholdValue);
5989 if (newDistance < filter) {
5990 float oldDistance = fabs(currentValue - thresholdValue);
5991 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005992 return true;
5993 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005994 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005995 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005996}
5997
Jeff Brown46b9ac02010-04-22 18:58:52 -07005998} // namespace android