michael@0: /* michael@0: * Copyright (C) 2010 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #define LOG_TAG "InputReader" michael@0: michael@0: //#define LOG_NDEBUG 0 michael@0: #include "cutils_log.h" michael@0: michael@0: // Log debug messages for each raw event received from the EventHub. michael@0: #define DEBUG_RAW_EVENTS 0 michael@0: michael@0: // Log debug messages about touch screen filtering hacks. michael@0: #define DEBUG_HACKS 0 michael@0: michael@0: // Log debug messages about virtual key processing. michael@0: #define DEBUG_VIRTUAL_KEYS 0 michael@0: michael@0: // Log debug messages about pointers. michael@0: #define DEBUG_POINTERS 0 michael@0: michael@0: // Log debug messages about pointer assignment calculations. michael@0: #define DEBUG_POINTER_ASSIGNMENT 0 michael@0: michael@0: // Log debug messages about gesture detection. michael@0: #define DEBUG_GESTURES 0 michael@0: michael@0: // Log debug messages about the vibrator. michael@0: #define DEBUG_VIBRATOR 0 michael@0: michael@0: #include "InputReader.h" michael@0: michael@0: #include "Keyboard.h" michael@0: #include "VirtualKeyMap.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define INDENT " " michael@0: #define INDENT2 " " michael@0: #define INDENT3 " " michael@0: #define INDENT4 " " michael@0: #define INDENT5 " " michael@0: michael@0: namespace android { michael@0: michael@0: // --- Constants --- michael@0: michael@0: // Maximum number of slots supported when using the slot-based Multitouch Protocol B. michael@0: static const size_t MAX_SLOTS = 32; michael@0: michael@0: // --- Static Functions --- michael@0: michael@0: template michael@0: inline static T abs(const T& value) { michael@0: return value < 0 ? - value : value; michael@0: } michael@0: michael@0: template michael@0: inline static T min(const T& a, const T& b) { michael@0: return a < b ? a : b; michael@0: } michael@0: michael@0: template michael@0: inline static void swap(T& a, T& b) { michael@0: T temp = a; michael@0: a = b; michael@0: b = temp; michael@0: } michael@0: michael@0: inline static float avg(float x, float y) { michael@0: return (x + y) / 2; michael@0: } michael@0: michael@0: inline static float distance(float x1, float y1, float x2, float y2) { michael@0: return hypotf(x1 - x2, y1 - y2); michael@0: } michael@0: michael@0: inline static int32_t signExtendNybble(int32_t value) { michael@0: return value >= 8 ? value - 16 : value; michael@0: } michael@0: michael@0: static inline const char* toString(bool value) { michael@0: return value ? "true" : "false"; michael@0: } michael@0: michael@0: static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation, michael@0: const int32_t map[][4], size_t mapSize) { michael@0: if (orientation != DISPLAY_ORIENTATION_0) { michael@0: for (size_t i = 0; i < mapSize; i++) { michael@0: if (value == map[i][0]) { michael@0: return map[i][orientation]; michael@0: } michael@0: } michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: static const int32_t keyCodeRotationMap[][4] = { michael@0: // key codes enumerated counter-clockwise with the original (unrotated) key first michael@0: // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation michael@0: { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT }, michael@0: { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN }, michael@0: { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT }, michael@0: { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP }, michael@0: }; michael@0: static const size_t keyCodeRotationMapSize = michael@0: sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]); michael@0: michael@0: static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) { michael@0: return rotateValueUsingRotationMap(keyCode, orientation, michael@0: keyCodeRotationMap, keyCodeRotationMapSize); michael@0: } michael@0: michael@0: static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) { michael@0: float temp; michael@0: switch (orientation) { michael@0: case DISPLAY_ORIENTATION_90: michael@0: temp = *deltaX; michael@0: *deltaX = *deltaY; michael@0: *deltaY = -temp; michael@0: break; michael@0: michael@0: case DISPLAY_ORIENTATION_180: michael@0: *deltaX = -*deltaX; michael@0: *deltaY = -*deltaY; michael@0: break; michael@0: michael@0: case DISPLAY_ORIENTATION_270: michael@0: temp = *deltaX; michael@0: *deltaX = -*deltaY; michael@0: *deltaY = temp; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) { michael@0: return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0; michael@0: } michael@0: michael@0: // Returns true if the pointer should be reported as being down given the specified michael@0: // button states. This determines whether the event is reported as a touch event. michael@0: static bool isPointerDown(int32_t buttonState) { michael@0: return buttonState & michael@0: (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY michael@0: | AMOTION_EVENT_BUTTON_TERTIARY); michael@0: } michael@0: michael@0: static float calculateCommonVector(float a, float b) { michael@0: if (a > 0 && b > 0) { michael@0: return a < b ? a : b; michael@0: } else if (a < 0 && b < 0) { michael@0: return a > b ? a : b; michael@0: } else { michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: static void synthesizeButtonKey(InputReaderContext* context, int32_t action, michael@0: nsecs_t when, int32_t deviceId, uint32_t source, michael@0: uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState, michael@0: int32_t buttonState, int32_t keyCode) { michael@0: if ( michael@0: (action == AKEY_EVENT_ACTION_DOWN michael@0: && !(lastButtonState & buttonState) michael@0: && (currentButtonState & buttonState)) michael@0: || (action == AKEY_EVENT_ACTION_UP michael@0: && (lastButtonState & buttonState) michael@0: && !(currentButtonState & buttonState))) { michael@0: NotifyKeyArgs args(when, deviceId, source, policyFlags, michael@0: action, 0, keyCode, 0, context->getGlobalMetaState(), when); michael@0: context->getListener()->notifyKey(&args); michael@0: } michael@0: } michael@0: michael@0: static void synthesizeButtonKeys(InputReaderContext* context, int32_t action, michael@0: nsecs_t when, int32_t deviceId, uint32_t source, michael@0: uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) { michael@0: synthesizeButtonKey(context, action, when, deviceId, source, policyFlags, michael@0: lastButtonState, currentButtonState, michael@0: AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK); michael@0: synthesizeButtonKey(context, action, when, deviceId, source, policyFlags, michael@0: lastButtonState, currentButtonState, michael@0: AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD); michael@0: } michael@0: michael@0: michael@0: // --- InputReaderConfiguration --- michael@0: michael@0: bool InputReaderConfiguration::getDisplayInfo(bool external, DisplayViewport* outViewport) const { michael@0: const DisplayViewport& viewport = external ? mExternalDisplay : mInternalDisplay; michael@0: if (viewport.displayId >= 0) { michael@0: *outViewport = viewport; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void InputReaderConfiguration::setDisplayInfo(bool external, const DisplayViewport& viewport) { michael@0: DisplayViewport& v = external ? mExternalDisplay : mInternalDisplay; michael@0: v = viewport; michael@0: } michael@0: michael@0: michael@0: // --- InputReader --- michael@0: michael@0: InputReader::InputReader(const sp& eventHub, michael@0: const sp& policy, michael@0: const sp& listener) : michael@0: mContext(this), mEventHub(eventHub), mPolicy(policy), michael@0: mGlobalMetaState(0), mGeneration(1), michael@0: mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX), michael@0: mConfigurationChangesToRefresh(0) { michael@0: mQueuedListener = new QueuedInputListener(listener); michael@0: michael@0: { // acquire lock michael@0: AutoMutex _l(mLock); michael@0: michael@0: refreshConfigurationLocked(0); michael@0: updateGlobalMetaStateLocked(); michael@0: } // release lock michael@0: } michael@0: michael@0: InputReader::~InputReader() { michael@0: for (size_t i = 0; i < mDevices.size(); i++) { michael@0: delete mDevices.valueAt(i); michael@0: } michael@0: } michael@0: michael@0: void InputReader::loopOnce() { michael@0: int32_t oldGeneration; michael@0: int32_t timeoutMillis; michael@0: bool inputDevicesChanged = false; michael@0: Vector inputDevices; michael@0: { // acquire lock michael@0: AutoMutex _l(mLock); michael@0: michael@0: oldGeneration = mGeneration; michael@0: timeoutMillis = -1; michael@0: michael@0: uint32_t changes = mConfigurationChangesToRefresh; michael@0: if (changes) { michael@0: mConfigurationChangesToRefresh = 0; michael@0: timeoutMillis = 0; michael@0: refreshConfigurationLocked(changes); michael@0: } else if (mNextTimeout != LLONG_MAX) { michael@0: nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); michael@0: timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout); michael@0: } michael@0: } // release lock michael@0: michael@0: size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE); michael@0: michael@0: { // acquire lock michael@0: AutoMutex _l(mLock); michael@0: mReaderIsAliveCondition.broadcast(); michael@0: michael@0: if (count) { michael@0: processEventsLocked(mEventBuffer, count); michael@0: } michael@0: michael@0: if (mNextTimeout != LLONG_MAX) { michael@0: nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); michael@0: if (now >= mNextTimeout) { michael@0: #if DEBUG_RAW_EVENTS michael@0: ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f); michael@0: #endif michael@0: mNextTimeout = LLONG_MAX; michael@0: timeoutExpiredLocked(now); michael@0: } michael@0: } michael@0: michael@0: if (oldGeneration != mGeneration) { michael@0: inputDevicesChanged = true; michael@0: getInputDevicesLocked(inputDevices); michael@0: } michael@0: } // release lock michael@0: michael@0: // Send out a message that the describes the changed input devices. michael@0: if (inputDevicesChanged) { michael@0: mPolicy->notifyInputDevicesChanged(inputDevices); michael@0: } michael@0: michael@0: // Flush queued events out to the listener. michael@0: // This must happen outside of the lock because the listener could potentially call michael@0: // back into the InputReader's methods, such as getScanCodeState, or become blocked michael@0: // on another thread similarly waiting to acquire the InputReader lock thereby michael@0: // resulting in a deadlock. This situation is actually quite plausible because the michael@0: // listener is actually the input dispatcher, which calls into the window manager, michael@0: // which occasionally calls into the input reader. michael@0: mQueuedListener->flush(); michael@0: } michael@0: michael@0: void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) { michael@0: for (const RawEvent* rawEvent = rawEvents; count;) { michael@0: int32_t type = rawEvent->type; michael@0: size_t batchSize = 1; michael@0: if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) { michael@0: int32_t deviceId = rawEvent->deviceId; michael@0: while (batchSize < count) { michael@0: if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT michael@0: || rawEvent[batchSize].deviceId != deviceId) { michael@0: break; michael@0: } michael@0: batchSize += 1; michael@0: } michael@0: #if DEBUG_RAW_EVENTS michael@0: ALOGD("BatchSize: %d Count: %d", batchSize, count); michael@0: #endif michael@0: processEventsForDeviceLocked(deviceId, rawEvent, batchSize); michael@0: } else { michael@0: switch (rawEvent->type) { michael@0: case EventHubInterface::DEVICE_ADDED: michael@0: addDeviceLocked(rawEvent->when, rawEvent->deviceId); michael@0: break; michael@0: case EventHubInterface::DEVICE_REMOVED: michael@0: removeDeviceLocked(rawEvent->when, rawEvent->deviceId); michael@0: break; michael@0: case EventHubInterface::FINISHED_DEVICE_SCAN: michael@0: handleConfigurationChangedLocked(rawEvent->when); michael@0: break; michael@0: default: michael@0: ALOG_ASSERT(false); // can't happen michael@0: break; michael@0: } michael@0: } michael@0: count -= batchSize; michael@0: rawEvent += batchSize; michael@0: } michael@0: } michael@0: michael@0: void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) { michael@0: ssize_t deviceIndex = mDevices.indexOfKey(deviceId); michael@0: if (deviceIndex >= 0) { michael@0: ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId); michael@0: return; michael@0: } michael@0: michael@0: InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId); michael@0: uint32_t classes = mEventHub->getDeviceClasses(deviceId); michael@0: michael@0: InputDevice* device = createDeviceLocked(deviceId, identifier, classes); michael@0: device->configure(when, &mConfig, 0); michael@0: device->reset(when); michael@0: michael@0: if (device->isIgnored()) { michael@0: ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, michael@0: identifier.name.string()); michael@0: } else { michael@0: ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, michael@0: identifier.name.string(), device->getSources()); michael@0: } michael@0: michael@0: mDevices.add(deviceId, device); michael@0: bumpGenerationLocked(); michael@0: } michael@0: michael@0: void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) { michael@0: InputDevice* device = NULL; michael@0: ssize_t deviceIndex = mDevices.indexOfKey(deviceId); michael@0: if (deviceIndex < 0) { michael@0: ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); michael@0: return; michael@0: } michael@0: michael@0: device = mDevices.valueAt(deviceIndex); michael@0: mDevices.removeItemsAt(deviceIndex, 1); michael@0: bumpGenerationLocked(); michael@0: michael@0: if (device->isIgnored()) { michael@0: ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)", michael@0: device->getId(), device->getName().string()); michael@0: } else { michael@0: ALOGI("Device removed: id=%d, name='%s', sources=0x%08x", michael@0: device->getId(), device->getName().string(), device->getSources()); michael@0: } michael@0: michael@0: device->reset(when); michael@0: delete device; michael@0: } michael@0: michael@0: InputDevice* InputReader::createDeviceLocked(int32_t deviceId, michael@0: const InputDeviceIdentifier& identifier, uint32_t classes) { michael@0: InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(), michael@0: identifier, classes); michael@0: michael@0: // External devices. michael@0: if (classes & INPUT_DEVICE_CLASS_EXTERNAL) { michael@0: device->setExternal(true); michael@0: } michael@0: michael@0: // Switch-like devices. michael@0: if (classes & INPUT_DEVICE_CLASS_SWITCH) { michael@0: device->addMapper(new SwitchInputMapper(device)); michael@0: } michael@0: michael@0: // Vibrator-like devices. michael@0: if (classes & INPUT_DEVICE_CLASS_VIBRATOR) { michael@0: device->addMapper(new VibratorInputMapper(device)); michael@0: } michael@0: michael@0: // Keyboard-like devices. michael@0: uint32_t keyboardSource = 0; michael@0: int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; michael@0: if (classes & INPUT_DEVICE_CLASS_KEYBOARD) { michael@0: keyboardSource |= AINPUT_SOURCE_KEYBOARD; michael@0: } michael@0: if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) { michael@0: keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; michael@0: } michael@0: if (classes & INPUT_DEVICE_CLASS_DPAD) { michael@0: keyboardSource |= AINPUT_SOURCE_DPAD; michael@0: } michael@0: if (classes & INPUT_DEVICE_CLASS_GAMEPAD) { michael@0: keyboardSource |= AINPUT_SOURCE_GAMEPAD; michael@0: } michael@0: michael@0: if (keyboardSource != 0) { michael@0: device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType)); michael@0: } michael@0: michael@0: // Cursor-like devices. michael@0: if (classes & INPUT_DEVICE_CLASS_CURSOR) { michael@0: device->addMapper(new CursorInputMapper(device)); michael@0: } michael@0: michael@0: // Touchscreens and touchpad devices. michael@0: if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) { michael@0: device->addMapper(new MultiTouchInputMapper(device)); michael@0: } else if (classes & INPUT_DEVICE_CLASS_TOUCH) { michael@0: device->addMapper(new SingleTouchInputMapper(device)); michael@0: } michael@0: michael@0: // Joystick-like devices. michael@0: if (classes & INPUT_DEVICE_CLASS_JOYSTICK) { michael@0: device->addMapper(new JoystickInputMapper(device)); michael@0: } michael@0: michael@0: return device; michael@0: } michael@0: michael@0: void InputReader::processEventsForDeviceLocked(int32_t deviceId, michael@0: const RawEvent* rawEvents, size_t count) { michael@0: ssize_t deviceIndex = mDevices.indexOfKey(deviceId); michael@0: if (deviceIndex < 0) { michael@0: ALOGW("Discarding event for unknown deviceId %d.", deviceId); michael@0: return; michael@0: } michael@0: michael@0: InputDevice* device = mDevices.valueAt(deviceIndex); michael@0: if (device->isIgnored()) { michael@0: //ALOGD("Discarding event for ignored deviceId %d.", deviceId); michael@0: return; michael@0: } michael@0: michael@0: device->process(rawEvents, count); michael@0: } michael@0: michael@0: void InputReader::timeoutExpiredLocked(nsecs_t when) { michael@0: for (size_t i = 0; i < mDevices.size(); i++) { michael@0: InputDevice* device = mDevices.valueAt(i); michael@0: if (!device->isIgnored()) { michael@0: device->timeoutExpired(when); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void InputReader::handleConfigurationChangedLocked(nsecs_t when) { michael@0: // Reset global meta state because it depends on the list of all configured devices. michael@0: updateGlobalMetaStateLocked(); michael@0: michael@0: // Enqueue configuration changed. michael@0: NotifyConfigurationChangedArgs args(when); michael@0: mQueuedListener->notifyConfigurationChanged(&args); michael@0: } michael@0: michael@0: void InputReader::refreshConfigurationLocked(uint32_t changes) { michael@0: mPolicy->getReaderConfiguration(&mConfig); michael@0: mEventHub->setExcludedDevices(mConfig.excludedDeviceNames); michael@0: michael@0: if (changes) { michael@0: ALOGI("Reconfiguring input devices. changes=0x%08x", changes); michael@0: nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); michael@0: michael@0: if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) { michael@0: mEventHub->requestReopenDevices(); michael@0: } else { michael@0: for (size_t i = 0; i < mDevices.size(); i++) { michael@0: InputDevice* device = mDevices.valueAt(i); michael@0: device->configure(now, &mConfig, changes); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void InputReader::updateGlobalMetaStateLocked() { michael@0: mGlobalMetaState = 0; michael@0: michael@0: for (size_t i = 0; i < mDevices.size(); i++) { michael@0: InputDevice* device = mDevices.valueAt(i); michael@0: mGlobalMetaState |= device->getMetaState(); michael@0: } michael@0: } michael@0: michael@0: int32_t InputReader::getGlobalMetaStateLocked() { michael@0: return mGlobalMetaState; michael@0: } michael@0: michael@0: void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) { michael@0: mDisableVirtualKeysTimeout = time; michael@0: } michael@0: michael@0: bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, michael@0: InputDevice* device, int32_t keyCode, int32_t scanCode) { michael@0: if (now < mDisableVirtualKeysTimeout) { michael@0: ALOGI("Dropping virtual key from device %s because virtual keys are " michael@0: "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", michael@0: device->getName().string(), michael@0: (mDisableVirtualKeysTimeout - now) * 0.000001, michael@0: keyCode, scanCode); michael@0: return true; michael@0: } else { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: void InputReader::fadePointerLocked() { michael@0: for (size_t i = 0; i < mDevices.size(); i++) { michael@0: InputDevice* device = mDevices.valueAt(i); michael@0: device->fadePointer(); michael@0: } michael@0: } michael@0: michael@0: void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) { michael@0: if (when < mNextTimeout) { michael@0: mNextTimeout = when; michael@0: mEventHub->wake(); michael@0: } michael@0: } michael@0: michael@0: int32_t InputReader::bumpGenerationLocked() { michael@0: return ++mGeneration; michael@0: } michael@0: michael@0: void InputReader::getInputDevices(Vector& outInputDevices) { michael@0: AutoMutex _l(mLock); michael@0: getInputDevicesLocked(outInputDevices); michael@0: } michael@0: michael@0: void InputReader::getInputDevicesLocked(Vector& outInputDevices) { michael@0: outInputDevices.clear(); michael@0: michael@0: size_t numDevices = mDevices.size(); michael@0: for (size_t i = 0; i < numDevices; i++) { michael@0: InputDevice* device = mDevices.valueAt(i); michael@0: if (!device->isIgnored()) { michael@0: outInputDevices.push(); michael@0: device->getDeviceInfo(&outInputDevices.editTop()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, michael@0: int32_t keyCode) { michael@0: AutoMutex _l(mLock); michael@0: michael@0: return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState); michael@0: } michael@0: michael@0: int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, michael@0: int32_t scanCode) { michael@0: AutoMutex _l(mLock); michael@0: michael@0: return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState); michael@0: } michael@0: michael@0: int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) { michael@0: AutoMutex _l(mLock); michael@0: michael@0: return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState); michael@0: } michael@0: michael@0: int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code, michael@0: GetStateFunc getStateFunc) { michael@0: int32_t result = AKEY_STATE_UNKNOWN; michael@0: if (deviceId >= 0) { michael@0: ssize_t deviceIndex = mDevices.indexOfKey(deviceId); michael@0: if (deviceIndex >= 0) { michael@0: InputDevice* device = mDevices.valueAt(deviceIndex); michael@0: if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { michael@0: result = (device->*getStateFunc)(sourceMask, code); michael@0: } michael@0: } michael@0: } else { michael@0: size_t numDevices = mDevices.size(); michael@0: for (size_t i = 0; i < numDevices; i++) { michael@0: InputDevice* device = mDevices.valueAt(i); michael@0: if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { michael@0: // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that michael@0: // value. Otherwise, return AKEY_STATE_UP as long as one device reports it. michael@0: int32_t currentResult = (device->*getStateFunc)(sourceMask, code); michael@0: if (currentResult >= AKEY_STATE_DOWN) { michael@0: return currentResult; michael@0: } else if (currentResult == AKEY_STATE_UP) { michael@0: result = currentResult; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, michael@0: size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { michael@0: AutoMutex _l(mLock); michael@0: michael@0: memset(outFlags, 0, numCodes); michael@0: return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags); michael@0: } michael@0: michael@0: bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, michael@0: size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { michael@0: bool result = false; michael@0: if (deviceId >= 0) { michael@0: ssize_t deviceIndex = mDevices.indexOfKey(deviceId); michael@0: if (deviceIndex >= 0) { michael@0: InputDevice* device = mDevices.valueAt(deviceIndex); michael@0: if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { michael@0: result = device->markSupportedKeyCodes(sourceMask, michael@0: numCodes, keyCodes, outFlags); michael@0: } michael@0: } michael@0: } else { michael@0: size_t numDevices = mDevices.size(); michael@0: for (size_t i = 0; i < numDevices; i++) { michael@0: InputDevice* device = mDevices.valueAt(i); michael@0: if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { michael@0: result |= device->markSupportedKeyCodes(sourceMask, michael@0: numCodes, keyCodes, outFlags); michael@0: } michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: void InputReader::requestRefreshConfiguration(uint32_t changes) { michael@0: AutoMutex _l(mLock); michael@0: michael@0: if (changes) { michael@0: bool needWake = !mConfigurationChangesToRefresh; michael@0: mConfigurationChangesToRefresh |= changes; michael@0: michael@0: if (needWake) { michael@0: mEventHub->wake(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize, michael@0: ssize_t repeat, int32_t token) { michael@0: AutoMutex _l(mLock); michael@0: michael@0: ssize_t deviceIndex = mDevices.indexOfKey(deviceId); michael@0: if (deviceIndex >= 0) { michael@0: InputDevice* device = mDevices.valueAt(deviceIndex); michael@0: device->vibrate(pattern, patternSize, repeat, token); michael@0: } michael@0: } michael@0: michael@0: void InputReader::cancelVibrate(int32_t deviceId, int32_t token) { michael@0: AutoMutex _l(mLock); michael@0: michael@0: ssize_t deviceIndex = mDevices.indexOfKey(deviceId); michael@0: if (deviceIndex >= 0) { michael@0: InputDevice* device = mDevices.valueAt(deviceIndex); michael@0: device->cancelVibrate(token); michael@0: } michael@0: } michael@0: michael@0: void InputReader::dump(String8& dump) { michael@0: AutoMutex _l(mLock); michael@0: michael@0: mEventHub->dump(dump); michael@0: dump.append("\n"); michael@0: michael@0: dump.append("Input Reader State:\n"); michael@0: michael@0: for (size_t i = 0; i < mDevices.size(); i++) { michael@0: mDevices.valueAt(i)->dump(dump); michael@0: } michael@0: michael@0: dump.append(INDENT "Configuration:\n"); michael@0: dump.append(INDENT2 "ExcludedDeviceNames: ["); michael@0: for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { michael@0: if (i != 0) { michael@0: dump.append(", "); michael@0: } michael@0: dump.append(mConfig.excludedDeviceNames.itemAt(i).string()); michael@0: } michael@0: dump.append("]\n"); michael@0: dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", michael@0: mConfig.virtualKeyQuietTime * 0.000001f); michael@0: michael@0: dump.appendFormat(INDENT2 "PointerVelocityControlParameters: " michael@0: "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", michael@0: mConfig.pointerVelocityControlParameters.scale, michael@0: mConfig.pointerVelocityControlParameters.lowThreshold, michael@0: mConfig.pointerVelocityControlParameters.highThreshold, michael@0: mConfig.pointerVelocityControlParameters.acceleration); michael@0: michael@0: dump.appendFormat(INDENT2 "WheelVelocityControlParameters: " michael@0: "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", michael@0: mConfig.wheelVelocityControlParameters.scale, michael@0: mConfig.wheelVelocityControlParameters.lowThreshold, michael@0: mConfig.wheelVelocityControlParameters.highThreshold, michael@0: mConfig.wheelVelocityControlParameters.acceleration); michael@0: michael@0: dump.appendFormat(INDENT2 "PointerGesture:\n"); michael@0: dump.appendFormat(INDENT3 "Enabled: %s\n", michael@0: toString(mConfig.pointerGesturesEnabled)); michael@0: dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n", michael@0: mConfig.pointerGestureQuietInterval * 0.000001f); michael@0: dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", michael@0: mConfig.pointerGestureDragMinSwitchSpeed); michael@0: dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n", michael@0: mConfig.pointerGestureTapInterval * 0.000001f); michael@0: dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n", michael@0: mConfig.pointerGestureTapDragInterval * 0.000001f); michael@0: dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n", michael@0: mConfig.pointerGestureTapSlop); michael@0: dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n", michael@0: mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); michael@0: dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n", michael@0: mConfig.pointerGestureMultitouchMinDistance); michael@0: dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", michael@0: mConfig.pointerGestureSwipeTransitionAngleCosine); michael@0: dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", michael@0: mConfig.pointerGestureSwipeMaxWidthRatio); michael@0: dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n", michael@0: mConfig.pointerGestureMovementSpeedRatio); michael@0: dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n", michael@0: mConfig.pointerGestureZoomSpeedRatio); michael@0: } michael@0: michael@0: void InputReader::monitor() { michael@0: // Acquire and release the lock to ensure that the reader has not deadlocked. michael@0: mLock.lock(); michael@0: mEventHub->wake(); michael@0: mReaderIsAliveCondition.wait(mLock); michael@0: mLock.unlock(); michael@0: michael@0: // Check the EventHub michael@0: mEventHub->monitor(); michael@0: } michael@0: michael@0: michael@0: // --- InputReader::ContextImpl --- michael@0: michael@0: InputReader::ContextImpl::ContextImpl(InputReader* reader) : michael@0: mReader(reader) { michael@0: } michael@0: michael@0: void InputReader::ContextImpl::updateGlobalMetaState() { michael@0: // lock is already held by the input loop michael@0: mReader->updateGlobalMetaStateLocked(); michael@0: } michael@0: michael@0: int32_t InputReader::ContextImpl::getGlobalMetaState() { michael@0: // lock is already held by the input loop michael@0: return mReader->getGlobalMetaStateLocked(); michael@0: } michael@0: michael@0: void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) { michael@0: // lock is already held by the input loop michael@0: mReader->disableVirtualKeysUntilLocked(time); michael@0: } michael@0: michael@0: bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, michael@0: InputDevice* device, int32_t keyCode, int32_t scanCode) { michael@0: // lock is already held by the input loop michael@0: return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode); michael@0: } michael@0: michael@0: void InputReader::ContextImpl::fadePointer() { michael@0: // lock is already held by the input loop michael@0: mReader->fadePointerLocked(); michael@0: } michael@0: michael@0: void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) { michael@0: // lock is already held by the input loop michael@0: mReader->requestTimeoutAtTimeLocked(when); michael@0: } michael@0: michael@0: int32_t InputReader::ContextImpl::bumpGeneration() { michael@0: // lock is already held by the input loop michael@0: return mReader->bumpGenerationLocked(); michael@0: } michael@0: michael@0: InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() { michael@0: return mReader->mPolicy.get(); michael@0: } michael@0: michael@0: InputListenerInterface* InputReader::ContextImpl::getListener() { michael@0: return mReader->mQueuedListener.get(); michael@0: } michael@0: michael@0: EventHubInterface* InputReader::ContextImpl::getEventHub() { michael@0: return mReader->mEventHub.get(); michael@0: } michael@0: michael@0: michael@0: // --- InputReaderThread --- michael@0: michael@0: InputReaderThread::InputReaderThread(const sp& reader) : michael@0: Thread(/*canCallJava*/ true), mReader(reader) { michael@0: } michael@0: michael@0: InputReaderThread::~InputReaderThread() { michael@0: } michael@0: michael@0: bool InputReaderThread::threadLoop() { michael@0: mReader->loopOnce(); michael@0: return true; michael@0: } michael@0: michael@0: michael@0: // --- InputDevice --- michael@0: michael@0: InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation, michael@0: const InputDeviceIdentifier& identifier, uint32_t classes) : michael@0: mContext(context), mId(id), mGeneration(generation), michael@0: mIdentifier(identifier), mClasses(classes), michael@0: mSources(0), mIsExternal(false), mDropUntilNextSync(false) { michael@0: } michael@0: michael@0: InputDevice::~InputDevice() { michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: delete mMappers[i]; michael@0: } michael@0: mMappers.clear(); michael@0: } michael@0: michael@0: void InputDevice::dump(String8& dump) { michael@0: InputDeviceInfo deviceInfo; michael@0: getDeviceInfo(& deviceInfo); michael@0: michael@0: dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), michael@0: deviceInfo.getDisplayName().string()); michael@0: dump.appendFormat(INDENT2 "Generation: %d\n", mGeneration); michael@0: dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); michael@0: dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); michael@0: dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); michael@0: michael@0: const Vector& ranges = deviceInfo.getMotionRanges(); michael@0: if (!ranges.isEmpty()) { michael@0: dump.append(INDENT2 "Motion Ranges:\n"); michael@0: for (size_t i = 0; i < ranges.size(); i++) { michael@0: const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); michael@0: const char* label = getAxisLabel(range.axis); michael@0: char name[32]; michael@0: if (label) { michael@0: strncpy(name, label, sizeof(name)); michael@0: name[sizeof(name) - 1] = '\0'; michael@0: } else { michael@0: snprintf(name, sizeof(name), "%d", range.axis); michael@0: } michael@0: dump.appendFormat(INDENT3 "%s: source=0x%08x, " michael@0: "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n", michael@0: name, range.source, range.min, range.max, range.flat, range.fuzz, michael@0: range.resolution); michael@0: } michael@0: } michael@0: michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: mapper->dump(dump); michael@0: } michael@0: } michael@0: michael@0: void InputDevice::addMapper(InputMapper* mapper) { michael@0: mMappers.add(mapper); michael@0: } michael@0: michael@0: void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) { michael@0: mSources = 0; michael@0: michael@0: if (!isIgnored()) { michael@0: if (!changes) { // first time only michael@0: mContext->getEventHub()->getConfiguration(mId, &mConfiguration); michael@0: } michael@0: michael@0: if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) { michael@0: if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) { michael@0: sp keyboardLayout = michael@0: mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier.descriptor); michael@0: if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) { michael@0: bumpGeneration(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) { michael@0: if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) { michael@0: String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier); michael@0: if (mAlias != alias) { michael@0: mAlias = alias; michael@0: bumpGeneration(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: mapper->configure(when, config, changes); michael@0: mSources |= mapper->getSources(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void InputDevice::reset(nsecs_t when) { michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: mapper->reset(when); michael@0: } michael@0: michael@0: mContext->updateGlobalMetaState(); michael@0: michael@0: notifyReset(when); michael@0: } michael@0: michael@0: void InputDevice::process(const RawEvent* rawEvents, size_t count) { michael@0: // Process all of the events in order for each mapper. michael@0: // We cannot simply ask each mapper to process them in bulk because mappers may michael@0: // have side-effects that must be interleaved. For example, joystick movement events and michael@0: // gamepad button presses are handled by different mappers but they should be dispatched michael@0: // in the order received. michael@0: size_t numMappers = mMappers.size(); michael@0: for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) { michael@0: #if DEBUG_RAW_EVENTS michael@0: ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%lld", michael@0: rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value, michael@0: rawEvent->when); michael@0: #endif michael@0: michael@0: if (mDropUntilNextSync) { michael@0: if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { michael@0: mDropUntilNextSync = false; michael@0: #if DEBUG_RAW_EVENTS michael@0: ALOGD("Recovered from input event buffer overrun."); michael@0: #endif michael@0: } else { michael@0: #if DEBUG_RAW_EVENTS michael@0: ALOGD("Dropped input event while waiting for next input sync."); michael@0: #endif michael@0: } michael@0: } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) { michael@0: ALOGI("Detected input event buffer overrun for device %s.", getName().string()); michael@0: mDropUntilNextSync = true; michael@0: reset(rawEvent->when); michael@0: } else { michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: mapper->process(rawEvent); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void InputDevice::timeoutExpired(nsecs_t when) { michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: mapper->timeoutExpired(when); michael@0: } michael@0: } michael@0: michael@0: void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { michael@0: outDeviceInfo->initialize(mId, mGeneration, mIdentifier, mAlias, mIsExternal); michael@0: michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: mapper->populateDeviceInfo(outDeviceInfo); michael@0: } michael@0: } michael@0: michael@0: int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { michael@0: return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); michael@0: } michael@0: michael@0: int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { michael@0: return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); michael@0: } michael@0: michael@0: int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { michael@0: return getState(sourceMask, switchCode, & InputMapper::getSwitchState); michael@0: } michael@0: michael@0: int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { michael@0: int32_t result = AKEY_STATE_UNKNOWN; michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: if (sourcesMatchMask(mapper->getSources(), sourceMask)) { michael@0: // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that michael@0: // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it. michael@0: int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code); michael@0: if (currentResult >= AKEY_STATE_DOWN) { michael@0: return currentResult; michael@0: } else if (currentResult == AKEY_STATE_UP) { michael@0: result = currentResult; michael@0: } michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, michael@0: const int32_t* keyCodes, uint8_t* outFlags) { michael@0: bool result = false; michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: if (sourcesMatchMask(mapper->getSources(), sourceMask)) { michael@0: result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, michael@0: int32_t token) { michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: mapper->vibrate(pattern, patternSize, repeat, token); michael@0: } michael@0: } michael@0: michael@0: void InputDevice::cancelVibrate(int32_t token) { michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: mapper->cancelVibrate(token); michael@0: } michael@0: } michael@0: michael@0: int32_t InputDevice::getMetaState() { michael@0: int32_t result = 0; michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: result |= mapper->getMetaState(); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: void InputDevice::fadePointer() { michael@0: size_t numMappers = mMappers.size(); michael@0: for (size_t i = 0; i < numMappers; i++) { michael@0: InputMapper* mapper = mMappers[i]; michael@0: mapper->fadePointer(); michael@0: } michael@0: } michael@0: michael@0: void InputDevice::bumpGeneration() { michael@0: mGeneration = mContext->bumpGeneration(); michael@0: } michael@0: michael@0: void InputDevice::notifyReset(nsecs_t when) { michael@0: NotifyDeviceResetArgs args(when, mId); michael@0: mContext->getListener()->notifyDeviceReset(&args); michael@0: } michael@0: michael@0: michael@0: // --- CursorButtonAccumulator --- michael@0: michael@0: CursorButtonAccumulator::CursorButtonAccumulator() { michael@0: clearButtons(); michael@0: } michael@0: michael@0: void CursorButtonAccumulator::reset(InputDevice* device) { michael@0: mBtnLeft = device->isKeyPressed(BTN_LEFT); michael@0: mBtnRight = device->isKeyPressed(BTN_RIGHT); michael@0: mBtnMiddle = device->isKeyPressed(BTN_MIDDLE); michael@0: mBtnBack = device->isKeyPressed(BTN_BACK); michael@0: mBtnSide = device->isKeyPressed(BTN_SIDE); michael@0: mBtnForward = device->isKeyPressed(BTN_FORWARD); michael@0: mBtnExtra = device->isKeyPressed(BTN_EXTRA); michael@0: mBtnTask = device->isKeyPressed(BTN_TASK); michael@0: } michael@0: michael@0: void CursorButtonAccumulator::clearButtons() { michael@0: mBtnLeft = 0; michael@0: mBtnRight = 0; michael@0: mBtnMiddle = 0; michael@0: mBtnBack = 0; michael@0: mBtnSide = 0; michael@0: mBtnForward = 0; michael@0: mBtnExtra = 0; michael@0: mBtnTask = 0; michael@0: } michael@0: michael@0: void CursorButtonAccumulator::process(const RawEvent* rawEvent) { michael@0: if (rawEvent->type == EV_KEY) { michael@0: switch (rawEvent->code) { michael@0: case BTN_LEFT: michael@0: mBtnLeft = rawEvent->value; michael@0: break; michael@0: case BTN_RIGHT: michael@0: mBtnRight = rawEvent->value; michael@0: break; michael@0: case BTN_MIDDLE: michael@0: mBtnMiddle = rawEvent->value; michael@0: break; michael@0: case BTN_BACK: michael@0: mBtnBack = rawEvent->value; michael@0: break; michael@0: case BTN_SIDE: michael@0: mBtnSide = rawEvent->value; michael@0: break; michael@0: case BTN_FORWARD: michael@0: mBtnForward = rawEvent->value; michael@0: break; michael@0: case BTN_EXTRA: michael@0: mBtnExtra = rawEvent->value; michael@0: break; michael@0: case BTN_TASK: michael@0: mBtnTask = rawEvent->value; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: uint32_t CursorButtonAccumulator::getButtonState() const { michael@0: uint32_t result = 0; michael@0: if (mBtnLeft) { michael@0: result |= AMOTION_EVENT_BUTTON_PRIMARY; michael@0: } michael@0: if (mBtnRight) { michael@0: result |= AMOTION_EVENT_BUTTON_SECONDARY; michael@0: } michael@0: if (mBtnMiddle) { michael@0: result |= AMOTION_EVENT_BUTTON_TERTIARY; michael@0: } michael@0: if (mBtnBack || mBtnSide) { michael@0: result |= AMOTION_EVENT_BUTTON_BACK; michael@0: } michael@0: if (mBtnForward || mBtnExtra) { michael@0: result |= AMOTION_EVENT_BUTTON_FORWARD; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: michael@0: // --- CursorMotionAccumulator --- michael@0: michael@0: CursorMotionAccumulator::CursorMotionAccumulator() { michael@0: clearRelativeAxes(); michael@0: } michael@0: michael@0: void CursorMotionAccumulator::reset(InputDevice* device) { michael@0: clearRelativeAxes(); michael@0: } michael@0: michael@0: void CursorMotionAccumulator::clearRelativeAxes() { michael@0: mRelX = 0; michael@0: mRelY = 0; michael@0: } michael@0: michael@0: void CursorMotionAccumulator::process(const RawEvent* rawEvent) { michael@0: if (rawEvent->type == EV_REL) { michael@0: switch (rawEvent->code) { michael@0: case REL_X: michael@0: mRelX = rawEvent->value; michael@0: break; michael@0: case REL_Y: michael@0: mRelY = rawEvent->value; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void CursorMotionAccumulator::finishSync() { michael@0: clearRelativeAxes(); michael@0: } michael@0: michael@0: michael@0: // --- CursorScrollAccumulator --- michael@0: michael@0: CursorScrollAccumulator::CursorScrollAccumulator() : michael@0: mHaveRelWheel(false), mHaveRelHWheel(false) { michael@0: clearRelativeAxes(); michael@0: } michael@0: michael@0: void CursorScrollAccumulator::configure(InputDevice* device) { michael@0: mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL); michael@0: mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL); michael@0: } michael@0: michael@0: void CursorScrollAccumulator::reset(InputDevice* device) { michael@0: clearRelativeAxes(); michael@0: } michael@0: michael@0: void CursorScrollAccumulator::clearRelativeAxes() { michael@0: mRelWheel = 0; michael@0: mRelHWheel = 0; michael@0: } michael@0: michael@0: void CursorScrollAccumulator::process(const RawEvent* rawEvent) { michael@0: if (rawEvent->type == EV_REL) { michael@0: switch (rawEvent->code) { michael@0: case REL_WHEEL: michael@0: mRelWheel = rawEvent->value; michael@0: break; michael@0: case REL_HWHEEL: michael@0: mRelHWheel = rawEvent->value; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void CursorScrollAccumulator::finishSync() { michael@0: clearRelativeAxes(); michael@0: } michael@0: michael@0: michael@0: // --- TouchButtonAccumulator --- michael@0: michael@0: TouchButtonAccumulator::TouchButtonAccumulator() : michael@0: mHaveBtnTouch(false), mHaveStylus(false) { michael@0: clearButtons(); michael@0: } michael@0: michael@0: void TouchButtonAccumulator::configure(InputDevice* device) { michael@0: mHaveBtnTouch = device->hasKey(BTN_TOUCH); michael@0: mHaveStylus = device->hasKey(BTN_TOOL_PEN) michael@0: || device->hasKey(BTN_TOOL_RUBBER) michael@0: || device->hasKey(BTN_TOOL_BRUSH) michael@0: || device->hasKey(BTN_TOOL_PENCIL) michael@0: || device->hasKey(BTN_TOOL_AIRBRUSH); michael@0: } michael@0: michael@0: void TouchButtonAccumulator::reset(InputDevice* device) { michael@0: mBtnTouch = device->isKeyPressed(BTN_TOUCH); michael@0: mBtnStylus = device->isKeyPressed(BTN_STYLUS); michael@0: mBtnStylus2 = device->isKeyPressed(BTN_STYLUS); michael@0: mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER); michael@0: mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN); michael@0: mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER); michael@0: mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH); michael@0: mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL); michael@0: mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH); michael@0: mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE); michael@0: mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS); michael@0: mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP); michael@0: mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP); michael@0: mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP); michael@0: } michael@0: michael@0: void TouchButtonAccumulator::clearButtons() { michael@0: mBtnTouch = 0; michael@0: mBtnStylus = 0; michael@0: mBtnStylus2 = 0; michael@0: mBtnToolFinger = 0; michael@0: mBtnToolPen = 0; michael@0: mBtnToolRubber = 0; michael@0: mBtnToolBrush = 0; michael@0: mBtnToolPencil = 0; michael@0: mBtnToolAirbrush = 0; michael@0: mBtnToolMouse = 0; michael@0: mBtnToolLens = 0; michael@0: mBtnToolDoubleTap = 0; michael@0: mBtnToolTripleTap = 0; michael@0: mBtnToolQuadTap = 0; michael@0: } michael@0: michael@0: void TouchButtonAccumulator::process(const RawEvent* rawEvent) { michael@0: if (rawEvent->type == EV_KEY) { michael@0: switch (rawEvent->code) { michael@0: case BTN_TOUCH: michael@0: mBtnTouch = rawEvent->value; michael@0: break; michael@0: case BTN_STYLUS: michael@0: mBtnStylus = rawEvent->value; michael@0: break; michael@0: case BTN_STYLUS2: michael@0: mBtnStylus2 = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_FINGER: michael@0: mBtnToolFinger = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_PEN: michael@0: mBtnToolPen = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_RUBBER: michael@0: mBtnToolRubber = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_BRUSH: michael@0: mBtnToolBrush = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_PENCIL: michael@0: mBtnToolPencil = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_AIRBRUSH: michael@0: mBtnToolAirbrush = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_MOUSE: michael@0: mBtnToolMouse = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_LENS: michael@0: mBtnToolLens = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_DOUBLETAP: michael@0: mBtnToolDoubleTap = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_TRIPLETAP: michael@0: mBtnToolTripleTap = rawEvent->value; michael@0: break; michael@0: case BTN_TOOL_QUADTAP: michael@0: mBtnToolQuadTap = rawEvent->value; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: uint32_t TouchButtonAccumulator::getButtonState() const { michael@0: uint32_t result = 0; michael@0: if (mBtnStylus) { michael@0: result |= AMOTION_EVENT_BUTTON_SECONDARY; michael@0: } michael@0: if (mBtnStylus2) { michael@0: result |= AMOTION_EVENT_BUTTON_TERTIARY; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: int32_t TouchButtonAccumulator::getToolType() const { michael@0: if (mBtnToolMouse || mBtnToolLens) { michael@0: return AMOTION_EVENT_TOOL_TYPE_MOUSE; michael@0: } michael@0: if (mBtnToolRubber) { michael@0: return AMOTION_EVENT_TOOL_TYPE_ERASER; michael@0: } michael@0: if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) { michael@0: return AMOTION_EVENT_TOOL_TYPE_STYLUS; michael@0: } michael@0: if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) { michael@0: return AMOTION_EVENT_TOOL_TYPE_FINGER; michael@0: } michael@0: return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; michael@0: } michael@0: michael@0: bool TouchButtonAccumulator::isToolActive() const { michael@0: return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber michael@0: || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush michael@0: || mBtnToolMouse || mBtnToolLens michael@0: || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap; michael@0: } michael@0: michael@0: bool TouchButtonAccumulator::isHovering() const { michael@0: return mHaveBtnTouch && !mBtnTouch; michael@0: } michael@0: michael@0: bool TouchButtonAccumulator::hasStylus() const { michael@0: return mHaveStylus; michael@0: } michael@0: michael@0: michael@0: // --- RawPointerAxes --- michael@0: michael@0: RawPointerAxes::RawPointerAxes() { michael@0: clear(); michael@0: } michael@0: michael@0: void RawPointerAxes::clear() { michael@0: x.clear(); michael@0: y.clear(); michael@0: pressure.clear(); michael@0: touchMajor.clear(); michael@0: touchMinor.clear(); michael@0: toolMajor.clear(); michael@0: toolMinor.clear(); michael@0: orientation.clear(); michael@0: distance.clear(); michael@0: tiltX.clear(); michael@0: tiltY.clear(); michael@0: trackingId.clear(); michael@0: slot.clear(); michael@0: } michael@0: michael@0: michael@0: // --- RawPointerData --- michael@0: michael@0: RawPointerData::RawPointerData() { michael@0: clear(); michael@0: } michael@0: michael@0: void RawPointerData::clear() { michael@0: pointerCount = 0; michael@0: clearIdBits(); michael@0: } michael@0: michael@0: void RawPointerData::copyFrom(const RawPointerData& other) { michael@0: pointerCount = other.pointerCount; michael@0: hoveringIdBits = other.hoveringIdBits; michael@0: touchingIdBits = other.touchingIdBits; michael@0: michael@0: for (uint32_t i = 0; i < pointerCount; i++) { michael@0: pointers[i] = other.pointers[i]; michael@0: michael@0: int id = pointers[i].id; michael@0: idToIndex[id] = other.idToIndex[id]; michael@0: } michael@0: } michael@0: michael@0: void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const { michael@0: float x = 0, y = 0; michael@0: uint32_t count = touchingIdBits.count(); michael@0: if (count) { michael@0: for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: const Pointer& pointer = pointerForId(id); michael@0: x += pointer.x; michael@0: y += pointer.y; michael@0: } michael@0: x /= count; michael@0: y /= count; michael@0: } michael@0: *outX = x; michael@0: *outY = y; michael@0: } michael@0: michael@0: michael@0: // --- CookedPointerData --- michael@0: michael@0: CookedPointerData::CookedPointerData() { michael@0: clear(); michael@0: } michael@0: michael@0: void CookedPointerData::clear() { michael@0: pointerCount = 0; michael@0: hoveringIdBits.clear(); michael@0: touchingIdBits.clear(); michael@0: } michael@0: michael@0: void CookedPointerData::copyFrom(const CookedPointerData& other) { michael@0: pointerCount = other.pointerCount; michael@0: hoveringIdBits = other.hoveringIdBits; michael@0: touchingIdBits = other.touchingIdBits; michael@0: michael@0: for (uint32_t i = 0; i < pointerCount; i++) { michael@0: pointerProperties[i].copyFrom(other.pointerProperties[i]); michael@0: pointerCoords[i].copyFrom(other.pointerCoords[i]); michael@0: michael@0: int id = pointerProperties[i].id; michael@0: idToIndex[id] = other.idToIndex[id]; michael@0: } michael@0: } michael@0: michael@0: michael@0: // --- SingleTouchMotionAccumulator --- michael@0: michael@0: SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() { michael@0: clearAbsoluteAxes(); michael@0: } michael@0: michael@0: void SingleTouchMotionAccumulator::reset(InputDevice* device) { michael@0: mAbsX = device->getAbsoluteAxisValue(ABS_X); michael@0: mAbsY = device->getAbsoluteAxisValue(ABS_Y); michael@0: mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE); michael@0: mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH); michael@0: mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE); michael@0: mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X); michael@0: mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y); michael@0: } michael@0: michael@0: void SingleTouchMotionAccumulator::clearAbsoluteAxes() { michael@0: mAbsX = 0; michael@0: mAbsY = 0; michael@0: mAbsPressure = 0; michael@0: mAbsToolWidth = 0; michael@0: mAbsDistance = 0; michael@0: mAbsTiltX = 0; michael@0: mAbsTiltY = 0; michael@0: } michael@0: michael@0: void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) { michael@0: if (rawEvent->type == EV_ABS) { michael@0: switch (rawEvent->code) { michael@0: case ABS_X: michael@0: mAbsX = rawEvent->value; michael@0: break; michael@0: case ABS_Y: michael@0: mAbsY = rawEvent->value; michael@0: break; michael@0: case ABS_PRESSURE: michael@0: mAbsPressure = rawEvent->value; michael@0: break; michael@0: case ABS_TOOL_WIDTH: michael@0: mAbsToolWidth = rawEvent->value; michael@0: break; michael@0: case ABS_DISTANCE: michael@0: mAbsDistance = rawEvent->value; michael@0: break; michael@0: case ABS_TILT_X: michael@0: mAbsTiltX = rawEvent->value; michael@0: break; michael@0: case ABS_TILT_Y: michael@0: mAbsTiltY = rawEvent->value; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: // --- MultiTouchMotionAccumulator --- michael@0: michael@0: MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() : michael@0: mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false), michael@0: mHaveStylus(false) { michael@0: } michael@0: michael@0: MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() { michael@0: delete[] mSlots; michael@0: } michael@0: michael@0: void MultiTouchMotionAccumulator::configure(InputDevice* device, michael@0: size_t slotCount, bool usingSlotsProtocol) { michael@0: mSlotCount = slotCount; michael@0: mUsingSlotsProtocol = usingSlotsProtocol; michael@0: mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE); michael@0: michael@0: delete[] mSlots; michael@0: mSlots = new Slot[slotCount]; michael@0: } michael@0: michael@0: void MultiTouchMotionAccumulator::reset(InputDevice* device) { michael@0: // Unfortunately there is no way to read the initial contents of the slots. michael@0: // So when we reset the accumulator, we must assume they are all zeroes. michael@0: if (mUsingSlotsProtocol) { michael@0: // Query the driver for the current slot index and use it as the initial slot michael@0: // before we start reading events from the device. It is possible that the michael@0: // current slot index will not be the same as it was when the first event was michael@0: // written into the evdev buffer, which means the input mapper could start michael@0: // out of sync with the initial state of the events in the evdev buffer. michael@0: // In the extremely unlikely case that this happens, the data from michael@0: // two slots will be confused until the next ABS_MT_SLOT event is received. michael@0: // This can cause the touch point to "jump", but at least there will be michael@0: // no stuck touches. michael@0: int32_t initialSlot; michael@0: status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(), michael@0: ABS_MT_SLOT, &initialSlot); michael@0: if (status) { michael@0: ALOGD("Could not retrieve current multitouch slot index. status=%d", status); michael@0: initialSlot = -1; michael@0: } michael@0: clearSlots(initialSlot); michael@0: } else { michael@0: clearSlots(-1); michael@0: } michael@0: } michael@0: michael@0: void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) { michael@0: if (mSlots) { michael@0: for (size_t i = 0; i < mSlotCount; i++) { michael@0: mSlots[i].clear(); michael@0: } michael@0: } michael@0: mCurrentSlot = initialSlot; michael@0: } michael@0: michael@0: void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) { michael@0: if (rawEvent->type == EV_ABS) { michael@0: bool newSlot = false; michael@0: if (mUsingSlotsProtocol) { michael@0: if (rawEvent->code == ABS_MT_SLOT) { michael@0: mCurrentSlot = rawEvent->value; michael@0: newSlot = true; michael@0: } michael@0: } else if (mCurrentSlot < 0) { michael@0: mCurrentSlot = 0; michael@0: } michael@0: michael@0: if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) { michael@0: #if DEBUG_POINTERS michael@0: if (newSlot) { michael@0: ALOGW("MultiTouch device emitted invalid slot index %d but it " michael@0: "should be between 0 and %d; ignoring this slot.", michael@0: mCurrentSlot, mSlotCount - 1); michael@0: } michael@0: #endif michael@0: } else { michael@0: Slot* slot = &mSlots[mCurrentSlot]; michael@0: michael@0: switch (rawEvent->code) { michael@0: case ABS_MT_POSITION_X: michael@0: slot->mInUse = true; michael@0: slot->mAbsMTPositionX = rawEvent->value; michael@0: break; michael@0: case ABS_MT_POSITION_Y: michael@0: slot->mInUse = true; michael@0: slot->mAbsMTPositionY = rawEvent->value; michael@0: break; michael@0: case ABS_MT_TOUCH_MAJOR: michael@0: slot->mInUse = true; michael@0: slot->mAbsMTTouchMajor = rawEvent->value; michael@0: break; michael@0: case ABS_MT_TOUCH_MINOR: michael@0: slot->mInUse = true; michael@0: slot->mAbsMTTouchMinor = rawEvent->value; michael@0: slot->mHaveAbsMTTouchMinor = true; michael@0: break; michael@0: case ABS_MT_WIDTH_MAJOR: michael@0: slot->mInUse = true; michael@0: slot->mAbsMTWidthMajor = rawEvent->value; michael@0: break; michael@0: case ABS_MT_WIDTH_MINOR: michael@0: slot->mInUse = true; michael@0: slot->mAbsMTWidthMinor = rawEvent->value; michael@0: slot->mHaveAbsMTWidthMinor = true; michael@0: break; michael@0: case ABS_MT_ORIENTATION: michael@0: slot->mInUse = true; michael@0: slot->mAbsMTOrientation = rawEvent->value; michael@0: break; michael@0: case ABS_MT_TRACKING_ID: michael@0: if (mUsingSlotsProtocol && rawEvent->value < 0) { michael@0: // The slot is no longer in use but it retains its previous contents, michael@0: // which may be reused for subsequent touches. michael@0: slot->mInUse = false; michael@0: } else { michael@0: slot->mInUse = true; michael@0: slot->mAbsMTTrackingId = rawEvent->value; michael@0: } michael@0: break; michael@0: case ABS_MT_PRESSURE: michael@0: slot->mInUse = true; michael@0: slot->mAbsMTPressure = rawEvent->value; michael@0: break; michael@0: case ABS_MT_DISTANCE: michael@0: slot->mInUse = true; michael@0: slot->mAbsMTDistance = rawEvent->value; michael@0: break; michael@0: case ABS_MT_TOOL_TYPE: michael@0: slot->mInUse = true; michael@0: slot->mAbsMTToolType = rawEvent->value; michael@0: slot->mHaveAbsMTToolType = true; michael@0: break; michael@0: } michael@0: } michael@0: } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) { michael@0: // MultiTouch Sync: The driver has returned all data for *one* of the pointers. michael@0: mCurrentSlot += 1; michael@0: } michael@0: } michael@0: michael@0: void MultiTouchMotionAccumulator::finishSync() { michael@0: if (!mUsingSlotsProtocol) { michael@0: clearSlots(-1); michael@0: } michael@0: } michael@0: michael@0: bool MultiTouchMotionAccumulator::hasStylus() const { michael@0: return mHaveStylus; michael@0: } michael@0: michael@0: michael@0: // --- MultiTouchMotionAccumulator::Slot --- michael@0: michael@0: MultiTouchMotionAccumulator::Slot::Slot() { michael@0: clear(); michael@0: } michael@0: michael@0: void MultiTouchMotionAccumulator::Slot::clear() { michael@0: mInUse = false; michael@0: mHaveAbsMTTouchMinor = false; michael@0: mHaveAbsMTWidthMinor = false; michael@0: mHaveAbsMTToolType = false; michael@0: mAbsMTPositionX = 0; michael@0: mAbsMTPositionY = 0; michael@0: mAbsMTTouchMajor = 0; michael@0: mAbsMTTouchMinor = 0; michael@0: mAbsMTWidthMajor = 0; michael@0: mAbsMTWidthMinor = 0; michael@0: mAbsMTOrientation = 0; michael@0: mAbsMTTrackingId = -1; michael@0: mAbsMTPressure = 0; michael@0: mAbsMTDistance = 0; michael@0: mAbsMTToolType = 0; michael@0: } michael@0: michael@0: int32_t MultiTouchMotionAccumulator::Slot::getToolType() const { michael@0: if (mHaveAbsMTToolType) { michael@0: switch (mAbsMTToolType) { michael@0: case MT_TOOL_FINGER: michael@0: return AMOTION_EVENT_TOOL_TYPE_FINGER; michael@0: case MT_TOOL_PEN: michael@0: return AMOTION_EVENT_TOOL_TYPE_STYLUS; michael@0: } michael@0: } michael@0: return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; michael@0: } michael@0: michael@0: michael@0: // --- InputMapper --- michael@0: michael@0: InputMapper::InputMapper(InputDevice* device) : michael@0: mDevice(device), mContext(device->getContext()) { michael@0: } michael@0: michael@0: InputMapper::~InputMapper() { michael@0: } michael@0: michael@0: void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { michael@0: info->addSource(getSources()); michael@0: } michael@0: michael@0: void InputMapper::dump(String8& dump) { michael@0: } michael@0: michael@0: void InputMapper::configure(nsecs_t when, michael@0: const InputReaderConfiguration* config, uint32_t changes) { michael@0: } michael@0: michael@0: void InputMapper::reset(nsecs_t when) { michael@0: } michael@0: michael@0: void InputMapper::timeoutExpired(nsecs_t when) { michael@0: } michael@0: michael@0: int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { michael@0: return AKEY_STATE_UNKNOWN; michael@0: } michael@0: michael@0: int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { michael@0: return AKEY_STATE_UNKNOWN; michael@0: } michael@0: michael@0: int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { michael@0: return AKEY_STATE_UNKNOWN; michael@0: } michael@0: michael@0: bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, michael@0: const int32_t* keyCodes, uint8_t* outFlags) { michael@0: return false; michael@0: } michael@0: michael@0: void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, michael@0: int32_t token) { michael@0: } michael@0: michael@0: void InputMapper::cancelVibrate(int32_t token) { michael@0: } michael@0: michael@0: int32_t InputMapper::getMetaState() { michael@0: return 0; michael@0: } michael@0: michael@0: void InputMapper::fadePointer() { michael@0: } michael@0: michael@0: status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) { michael@0: return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo); michael@0: } michael@0: michael@0: void InputMapper::bumpGeneration() { michael@0: mDevice->bumpGeneration(); michael@0: } michael@0: michael@0: void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, michael@0: const RawAbsoluteAxisInfo& axis, const char* name) { michael@0: if (axis.valid) { michael@0: dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n", michael@0: name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution); michael@0: } else { michael@0: dump.appendFormat(INDENT4 "%s: unknown range\n", name); michael@0: } michael@0: } michael@0: michael@0: michael@0: // --- SwitchInputMapper --- michael@0: michael@0: SwitchInputMapper::SwitchInputMapper(InputDevice* device) : michael@0: InputMapper(device), mUpdatedSwitchValues(0), mUpdatedSwitchMask(0) { michael@0: } michael@0: michael@0: SwitchInputMapper::~SwitchInputMapper() { michael@0: } michael@0: michael@0: uint32_t SwitchInputMapper::getSources() { michael@0: return AINPUT_SOURCE_SWITCH; michael@0: } michael@0: michael@0: void SwitchInputMapper::process(const RawEvent* rawEvent) { michael@0: switch (rawEvent->type) { michael@0: case EV_SW: michael@0: processSwitch(rawEvent->code, rawEvent->value); michael@0: break; michael@0: michael@0: case EV_SYN: michael@0: if (rawEvent->code == SYN_REPORT) { michael@0: sync(rawEvent->when); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) { michael@0: if (switchCode >= 0 && switchCode < 32) { michael@0: if (switchValue) { michael@0: mUpdatedSwitchValues |= 1 << switchCode; michael@0: } michael@0: mUpdatedSwitchMask |= 1 << switchCode; michael@0: } michael@0: } michael@0: michael@0: void SwitchInputMapper::sync(nsecs_t when) { michael@0: if (mUpdatedSwitchMask) { michael@0: NotifySwitchArgs args(when, 0, mUpdatedSwitchValues, mUpdatedSwitchMask); michael@0: getListener()->notifySwitch(&args); michael@0: michael@0: mUpdatedSwitchValues = 0; michael@0: mUpdatedSwitchMask = 0; michael@0: } michael@0: } michael@0: michael@0: int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { michael@0: return getEventHub()->getSwitchState(getDeviceId(), switchCode); michael@0: } michael@0: michael@0: michael@0: // --- VibratorInputMapper --- michael@0: michael@0: VibratorInputMapper::VibratorInputMapper(InputDevice* device) : michael@0: InputMapper(device), mVibrating(false) { michael@0: } michael@0: michael@0: VibratorInputMapper::~VibratorInputMapper() { michael@0: } michael@0: michael@0: uint32_t VibratorInputMapper::getSources() { michael@0: return 0; michael@0: } michael@0: michael@0: void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { michael@0: InputMapper::populateDeviceInfo(info); michael@0: michael@0: info->setVibrator(true); michael@0: } michael@0: michael@0: void VibratorInputMapper::process(const RawEvent* rawEvent) { michael@0: // TODO: Handle FF_STATUS, although it does not seem to be widely supported. michael@0: } michael@0: michael@0: void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, michael@0: int32_t token) { michael@0: #if DEBUG_VIBRATOR michael@0: String8 patternStr; michael@0: for (size_t i = 0; i < patternSize; i++) { michael@0: if (i != 0) { michael@0: patternStr.append(", "); michael@0: } michael@0: patternStr.appendFormat("%lld", pattern[i]); michael@0: } michael@0: ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d", michael@0: getDeviceId(), patternStr.string(), repeat, token); michael@0: #endif michael@0: michael@0: mVibrating = true; michael@0: memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t)); michael@0: mPatternSize = patternSize; michael@0: mRepeat = repeat; michael@0: mToken = token; michael@0: mIndex = -1; michael@0: michael@0: nextStep(); michael@0: } michael@0: michael@0: void VibratorInputMapper::cancelVibrate(int32_t token) { michael@0: #if DEBUG_VIBRATOR michael@0: ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token); michael@0: #endif michael@0: michael@0: if (mVibrating && mToken == token) { michael@0: stopVibrating(); michael@0: } michael@0: } michael@0: michael@0: void VibratorInputMapper::timeoutExpired(nsecs_t when) { michael@0: if (mVibrating) { michael@0: if (when >= mNextStepTime) { michael@0: nextStep(); michael@0: } else { michael@0: getContext()->requestTimeoutAtTime(mNextStepTime); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void VibratorInputMapper::nextStep() { michael@0: mIndex += 1; michael@0: if (size_t(mIndex) >= mPatternSize) { michael@0: if (mRepeat < 0) { michael@0: // We are done. michael@0: stopVibrating(); michael@0: return; michael@0: } michael@0: mIndex = mRepeat; michael@0: } michael@0: michael@0: bool vibratorOn = mIndex & 1; michael@0: nsecs_t duration = mPattern[mIndex]; michael@0: if (vibratorOn) { michael@0: #if DEBUG_VIBRATOR michael@0: ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld", michael@0: getDeviceId(), duration); michael@0: #endif michael@0: getEventHub()->vibrate(getDeviceId(), duration); michael@0: } else { michael@0: #if DEBUG_VIBRATOR michael@0: ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId()); michael@0: #endif michael@0: getEventHub()->cancelVibrate(getDeviceId()); michael@0: } michael@0: nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); michael@0: mNextStepTime = now + duration; michael@0: getContext()->requestTimeoutAtTime(mNextStepTime); michael@0: #if DEBUG_VIBRATOR michael@0: ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f); michael@0: #endif michael@0: } michael@0: michael@0: void VibratorInputMapper::stopVibrating() { michael@0: mVibrating = false; michael@0: #if DEBUG_VIBRATOR michael@0: ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId()); michael@0: #endif michael@0: getEventHub()->cancelVibrate(getDeviceId()); michael@0: } michael@0: michael@0: void VibratorInputMapper::dump(String8& dump) { michael@0: dump.append(INDENT2 "Vibrator Input Mapper:\n"); michael@0: dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating)); michael@0: } michael@0: michael@0: michael@0: // --- KeyboardInputMapper --- michael@0: michael@0: KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, michael@0: uint32_t source, int32_t keyboardType) : michael@0: InputMapper(device), mSource(source), michael@0: mKeyboardType(keyboardType) { michael@0: } michael@0: michael@0: KeyboardInputMapper::~KeyboardInputMapper() { michael@0: } michael@0: michael@0: uint32_t KeyboardInputMapper::getSources() { michael@0: return mSource; michael@0: } michael@0: michael@0: void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { michael@0: InputMapper::populateDeviceInfo(info); michael@0: michael@0: info->setKeyboardType(mKeyboardType); michael@0: info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId())); michael@0: } michael@0: michael@0: void KeyboardInputMapper::dump(String8& dump) { michael@0: dump.append(INDENT2 "Keyboard Input Mapper:\n"); michael@0: dumpParameters(dump); michael@0: dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); michael@0: dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); michael@0: dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size()); michael@0: dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState); michael@0: dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); michael@0: } michael@0: michael@0: michael@0: void KeyboardInputMapper::configure(nsecs_t when, michael@0: const InputReaderConfiguration* config, uint32_t changes) { michael@0: InputMapper::configure(when, config, changes); michael@0: michael@0: if (!changes) { // first time only michael@0: // Configure basic parameters. michael@0: configureParameters(); michael@0: } michael@0: michael@0: if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { michael@0: if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) { michael@0: DisplayViewport v; michael@0: if (config->getDisplayInfo(false /*external*/, &v)) { michael@0: mOrientation = v.orientation; michael@0: } else { michael@0: mOrientation = DISPLAY_ORIENTATION_0; michael@0: } michael@0: } else { michael@0: mOrientation = DISPLAY_ORIENTATION_0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void KeyboardInputMapper::configureParameters() { michael@0: mParameters.orientationAware = false; michael@0: getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), michael@0: mParameters.orientationAware); michael@0: michael@0: mParameters.hasAssociatedDisplay = false; michael@0: if (mParameters.orientationAware) { michael@0: mParameters.hasAssociatedDisplay = true; michael@0: } michael@0: } michael@0: michael@0: void KeyboardInputMapper::dumpParameters(String8& dump) { michael@0: dump.append(INDENT3 "Parameters:\n"); michael@0: dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n", michael@0: toString(mParameters.hasAssociatedDisplay)); michael@0: dump.appendFormat(INDENT4 "OrientationAware: %s\n", michael@0: toString(mParameters.orientationAware)); michael@0: } michael@0: michael@0: void KeyboardInputMapper::reset(nsecs_t when) { michael@0: mMetaState = AMETA_NONE; michael@0: mDownTime = 0; michael@0: mKeyDowns.clear(); michael@0: mCurrentHidUsage = 0; michael@0: michael@0: resetLedState(); michael@0: michael@0: InputMapper::reset(when); michael@0: } michael@0: michael@0: void KeyboardInputMapper::process(const RawEvent* rawEvent) { michael@0: switch (rawEvent->type) { michael@0: case EV_KEY: { michael@0: int32_t scanCode = rawEvent->code; michael@0: int32_t usageCode = mCurrentHidUsage; michael@0: mCurrentHidUsage = 0; michael@0: michael@0: if (isKeyboardOrGamepadKey(scanCode)) { michael@0: int32_t keyCode; michael@0: uint32_t flags; michael@0: if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) { michael@0: keyCode = AKEYCODE_UNKNOWN; michael@0: flags = 0; michael@0: } michael@0: processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags); michael@0: } michael@0: break; michael@0: } michael@0: case EV_MSC: { michael@0: if (rawEvent->code == MSC_SCAN) { michael@0: mCurrentHidUsage = rawEvent->value; michael@0: } michael@0: break; michael@0: } michael@0: case EV_SYN: { michael@0: if (rawEvent->code == SYN_REPORT) { michael@0: mCurrentHidUsage = 0; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { michael@0: return scanCode < BTN_MOUSE michael@0: || scanCode >= KEY_OK michael@0: || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) michael@0: || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); michael@0: } michael@0: michael@0: void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, michael@0: int32_t scanCode, uint32_t policyFlags) { michael@0: michael@0: if (down) { michael@0: // Rotate key codes according to orientation if needed. michael@0: if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) { michael@0: keyCode = rotateKeyCode(keyCode, mOrientation); michael@0: } michael@0: michael@0: // Add key down. michael@0: ssize_t keyDownIndex = findKeyDown(scanCode); michael@0: if (keyDownIndex >= 0) { michael@0: // key repeat, be sure to use same keycode as before in case of rotation michael@0: keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; michael@0: } else { michael@0: // key down michael@0: if ((policyFlags & POLICY_FLAG_VIRTUAL) michael@0: && mContext->shouldDropVirtualKey(when, michael@0: getDevice(), keyCode, scanCode)) { michael@0: return; michael@0: } michael@0: michael@0: mKeyDowns.push(); michael@0: KeyDown& keyDown = mKeyDowns.editTop(); michael@0: keyDown.keyCode = keyCode; michael@0: keyDown.scanCode = scanCode; michael@0: } michael@0: michael@0: mDownTime = when; michael@0: } else { michael@0: // Remove key down. michael@0: ssize_t keyDownIndex = findKeyDown(scanCode); michael@0: if (keyDownIndex >= 0) { michael@0: // key up, be sure to use same keycode as before in case of rotation michael@0: keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; michael@0: mKeyDowns.removeAt(size_t(keyDownIndex)); michael@0: } else { michael@0: // key was not actually down michael@0: ALOGI("Dropping key up from device %s because the key was not down. " michael@0: "keyCode=%d, scanCode=%d", michael@0: getDeviceName().string(), keyCode, scanCode); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: bool metaStateChanged = false; michael@0: int32_t oldMetaState = mMetaState; michael@0: int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState); michael@0: if (oldMetaState != newMetaState) { michael@0: mMetaState = newMetaState; michael@0: metaStateChanged = true; michael@0: updateLedState(false); michael@0: } michael@0: michael@0: nsecs_t downTime = mDownTime; michael@0: michael@0: // Key down on external an keyboard should wake the device. michael@0: // We don't do this for internal keyboards to prevent them from waking up in your pocket. michael@0: // For internal keyboards, the key layout file should specify the policy flags for michael@0: // each wake key individually. michael@0: // TODO: Use the input device configuration to control this behavior more finely. michael@0: if (down && getDevice()->isExternal() michael@0: && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { michael@0: policyFlags |= POLICY_FLAG_WAKE_DROPPED; michael@0: } michael@0: michael@0: if (metaStateChanged) { michael@0: getContext()->updateGlobalMetaState(); michael@0: } michael@0: michael@0: if (down && !isMetaKey(keyCode)) { michael@0: getContext()->fadePointer(); michael@0: } michael@0: michael@0: NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags, michael@0: down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, michael@0: AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); michael@0: getListener()->notifyKey(&args); michael@0: } michael@0: michael@0: ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) { michael@0: size_t n = mKeyDowns.size(); michael@0: for (size_t i = 0; i < n; i++) { michael@0: if (mKeyDowns[i].scanCode == scanCode) { michael@0: return i; michael@0: } michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { michael@0: return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); michael@0: } michael@0: michael@0: int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { michael@0: return getEventHub()->getScanCodeState(getDeviceId(), scanCode); michael@0: } michael@0: michael@0: bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, michael@0: const int32_t* keyCodes, uint8_t* outFlags) { michael@0: return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); michael@0: } michael@0: michael@0: int32_t KeyboardInputMapper::getMetaState() { michael@0: return mMetaState; michael@0: } michael@0: michael@0: void KeyboardInputMapper::resetLedState() { michael@0: initializeLedState(mCapsLockLedState, LED_CAPSL); michael@0: initializeLedState(mNumLockLedState, LED_NUML); michael@0: initializeLedState(mScrollLockLedState, LED_SCROLLL); michael@0: michael@0: updateLedState(true); michael@0: } michael@0: michael@0: void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) { michael@0: ledState.avail = getEventHub()->hasLed(getDeviceId(), led); michael@0: ledState.on = false; michael@0: } michael@0: michael@0: void KeyboardInputMapper::updateLedState(bool reset) { michael@0: updateLedStateForModifier(mCapsLockLedState, LED_CAPSL, michael@0: AMETA_CAPS_LOCK_ON, reset); michael@0: updateLedStateForModifier(mNumLockLedState, LED_NUML, michael@0: AMETA_NUM_LOCK_ON, reset); michael@0: updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL, michael@0: AMETA_SCROLL_LOCK_ON, reset); michael@0: } michael@0: michael@0: void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, michael@0: int32_t led, int32_t modifier, bool reset) { michael@0: if (ledState.avail) { michael@0: bool desiredState = (mMetaState & modifier) != 0; michael@0: if (reset || ledState.on != desiredState) { michael@0: getEventHub()->setLedState(getDeviceId(), led, desiredState); michael@0: ledState.on = desiredState; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: // --- CursorInputMapper --- michael@0: michael@0: CursorInputMapper::CursorInputMapper(InputDevice* device) : michael@0: InputMapper(device) { michael@0: } michael@0: michael@0: CursorInputMapper::~CursorInputMapper() { michael@0: } michael@0: michael@0: uint32_t CursorInputMapper::getSources() { michael@0: return mSource; michael@0: } michael@0: michael@0: void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { michael@0: InputMapper::populateDeviceInfo(info); michael@0: michael@0: if (mParameters.mode == Parameters::MODE_POINTER) { michael@0: float minX, minY, maxX, maxY; michael@0: if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f, 0.0f); michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f, 0.0f); michael@0: } michael@0: } else { michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale, 0.0f); michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale, 0.0f); michael@0: } michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); michael@0: michael@0: if (mCursorScrollAccumulator.haveRelativeVWheel()) { michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f); michael@0: } michael@0: if (mCursorScrollAccumulator.haveRelativeHWheel()) { michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f); michael@0: } michael@0: } michael@0: michael@0: void CursorInputMapper::dump(String8& dump) { michael@0: dump.append(INDENT2 "Cursor Input Mapper:\n"); michael@0: dumpParameters(dump); michael@0: dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); michael@0: dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); michael@0: dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); michael@0: dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); michael@0: dump.appendFormat(INDENT3 "HaveVWheel: %s\n", michael@0: toString(mCursorScrollAccumulator.haveRelativeVWheel())); michael@0: dump.appendFormat(INDENT3 "HaveHWheel: %s\n", michael@0: toString(mCursorScrollAccumulator.haveRelativeHWheel())); michael@0: dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); michael@0: dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); michael@0: dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); michael@0: dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState); michael@0: dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState))); michael@0: dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); michael@0: } michael@0: michael@0: void CursorInputMapper::configure(nsecs_t when, michael@0: const InputReaderConfiguration* config, uint32_t changes) { michael@0: InputMapper::configure(when, config, changes); michael@0: michael@0: if (!changes) { // first time only michael@0: mCursorScrollAccumulator.configure(getDevice()); michael@0: michael@0: // Configure basic parameters. michael@0: configureParameters(); michael@0: michael@0: // Configure device mode. michael@0: switch (mParameters.mode) { michael@0: case Parameters::MODE_POINTER: michael@0: mSource = AINPUT_SOURCE_MOUSE; michael@0: mXPrecision = 1.0f; michael@0: mYPrecision = 1.0f; michael@0: mXScale = 1.0f; michael@0: mYScale = 1.0f; michael@0: mPointerController = getPolicy()->obtainPointerController(getDeviceId()); michael@0: break; michael@0: case Parameters::MODE_NAVIGATION: michael@0: mSource = AINPUT_SOURCE_TRACKBALL; michael@0: mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; michael@0: mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; michael@0: mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; michael@0: mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; michael@0: break; michael@0: } michael@0: michael@0: mVWheelScale = 1.0f; michael@0: mHWheelScale = 1.0f; michael@0: } michael@0: michael@0: if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { michael@0: mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters); michael@0: mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters); michael@0: mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters); michael@0: } michael@0: michael@0: if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { michael@0: if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) { michael@0: DisplayViewport v; michael@0: if (config->getDisplayInfo(false /*external*/, &v)) { michael@0: mOrientation = v.orientation; michael@0: } else { michael@0: mOrientation = DISPLAY_ORIENTATION_0; michael@0: } michael@0: } else { michael@0: mOrientation = DISPLAY_ORIENTATION_0; michael@0: } michael@0: bumpGeneration(); michael@0: } michael@0: } michael@0: michael@0: void CursorInputMapper::configureParameters() { michael@0: mParameters.mode = Parameters::MODE_POINTER; michael@0: String8 cursorModeString; michael@0: if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { michael@0: if (cursorModeString == "navigation") { michael@0: mParameters.mode = Parameters::MODE_NAVIGATION; michael@0: } else if (cursorModeString != "pointer" && cursorModeString != "default") { michael@0: ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); michael@0: } michael@0: } michael@0: michael@0: mParameters.orientationAware = false; michael@0: getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), michael@0: mParameters.orientationAware); michael@0: michael@0: mParameters.hasAssociatedDisplay = false; michael@0: if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) { michael@0: mParameters.hasAssociatedDisplay = true; michael@0: } michael@0: } michael@0: michael@0: void CursorInputMapper::dumpParameters(String8& dump) { michael@0: dump.append(INDENT3 "Parameters:\n"); michael@0: dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n", michael@0: toString(mParameters.hasAssociatedDisplay)); michael@0: michael@0: switch (mParameters.mode) { michael@0: case Parameters::MODE_POINTER: michael@0: dump.append(INDENT4 "Mode: pointer\n"); michael@0: break; michael@0: case Parameters::MODE_NAVIGATION: michael@0: dump.append(INDENT4 "Mode: navigation\n"); michael@0: break; michael@0: default: michael@0: ALOG_ASSERT(false); michael@0: } michael@0: michael@0: dump.appendFormat(INDENT4 "OrientationAware: %s\n", michael@0: toString(mParameters.orientationAware)); michael@0: } michael@0: michael@0: void CursorInputMapper::reset(nsecs_t when) { michael@0: mButtonState = 0; michael@0: mDownTime = 0; michael@0: michael@0: mPointerVelocityControl.reset(); michael@0: mWheelXVelocityControl.reset(); michael@0: mWheelYVelocityControl.reset(); michael@0: michael@0: mCursorButtonAccumulator.reset(getDevice()); michael@0: mCursorMotionAccumulator.reset(getDevice()); michael@0: mCursorScrollAccumulator.reset(getDevice()); michael@0: michael@0: InputMapper::reset(when); michael@0: } michael@0: michael@0: void CursorInputMapper::process(const RawEvent* rawEvent) { michael@0: mCursorButtonAccumulator.process(rawEvent); michael@0: mCursorMotionAccumulator.process(rawEvent); michael@0: mCursorScrollAccumulator.process(rawEvent); michael@0: michael@0: if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { michael@0: sync(rawEvent->when); michael@0: } michael@0: } michael@0: michael@0: void CursorInputMapper::sync(nsecs_t when) { michael@0: int32_t lastButtonState = mButtonState; michael@0: int32_t currentButtonState = mCursorButtonAccumulator.getButtonState(); michael@0: mButtonState = currentButtonState; michael@0: michael@0: bool wasDown = isPointerDown(lastButtonState); michael@0: bool down = isPointerDown(currentButtonState); michael@0: bool downChanged; michael@0: if (!wasDown && down) { michael@0: mDownTime = when; michael@0: downChanged = true; michael@0: } else if (wasDown && !down) { michael@0: downChanged = true; michael@0: } else { michael@0: downChanged = false; michael@0: } michael@0: nsecs_t downTime = mDownTime; michael@0: bool buttonsChanged = currentButtonState != lastButtonState; michael@0: bool buttonsPressed = currentButtonState & ~lastButtonState; michael@0: michael@0: float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale; michael@0: float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale; michael@0: bool moved = deltaX != 0 || deltaY != 0; michael@0: michael@0: // Rotate delta according to orientation if needed. michael@0: if (mParameters.orientationAware && mParameters.hasAssociatedDisplay michael@0: && (deltaX != 0.0f || deltaY != 0.0f)) { michael@0: rotateDelta(mOrientation, &deltaX, &deltaY); michael@0: } michael@0: michael@0: // Move the pointer. michael@0: PointerProperties pointerProperties; michael@0: pointerProperties.clear(); michael@0: pointerProperties.id = 0; michael@0: pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE; michael@0: michael@0: PointerCoords pointerCoords; michael@0: pointerCoords.clear(); michael@0: michael@0: float vscroll = mCursorScrollAccumulator.getRelativeVWheel(); michael@0: float hscroll = mCursorScrollAccumulator.getRelativeHWheel(); michael@0: bool scrolled = vscroll != 0 || hscroll != 0; michael@0: michael@0: mWheelYVelocityControl.move(when, NULL, &vscroll); michael@0: mWheelXVelocityControl.move(when, &hscroll, NULL); michael@0: michael@0: mPointerVelocityControl.move(when, &deltaX, &deltaY); michael@0: michael@0: int32_t displayId; michael@0: if (mPointerController != NULL) { michael@0: if (moved || scrolled || buttonsChanged) { michael@0: mPointerController->setPresentation( michael@0: PointerControllerInterface::PRESENTATION_POINTER); michael@0: michael@0: if (moved) { michael@0: mPointerController->move(deltaX, deltaY); michael@0: } michael@0: michael@0: if (buttonsChanged) { michael@0: mPointerController->setButtonState(currentButtonState); michael@0: } michael@0: michael@0: mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); michael@0: } michael@0: michael@0: float x, y; michael@0: mPointerController->getPosition(&x, &y); michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); michael@0: displayId = ADISPLAY_ID_DEFAULT; michael@0: } else { michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); michael@0: displayId = ADISPLAY_ID_NONE; michael@0: } michael@0: michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); michael@0: michael@0: // Moving an external trackball or mouse should wake the device. michael@0: // We don't do this for internal cursor devices to prevent them from waking up michael@0: // the device in your pocket. michael@0: // TODO: Use the input device configuration to control this behavior more finely. michael@0: uint32_t policyFlags = 0; michael@0: if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) { michael@0: policyFlags |= POLICY_FLAG_WAKE_DROPPED; michael@0: } michael@0: michael@0: // Synthesize key down from buttons if needed. michael@0: synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, michael@0: policyFlags, lastButtonState, currentButtonState); michael@0: michael@0: // Send motion event. michael@0: if (downChanged || moved || scrolled || buttonsChanged) { michael@0: int32_t metaState = mContext->getGlobalMetaState(); michael@0: int32_t motionEventAction; michael@0: if (downChanged) { michael@0: motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; michael@0: } else if (down || mPointerController == NULL) { michael@0: motionEventAction = AMOTION_EVENT_ACTION_MOVE; michael@0: } else { michael@0: motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; michael@0: } michael@0: michael@0: NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, michael@0: motionEventAction, 0, metaState, currentButtonState, 0, michael@0: displayId, 1, &pointerProperties, &pointerCoords, michael@0: mXPrecision, mYPrecision, downTime); michael@0: getListener()->notifyMotion(&args); michael@0: michael@0: // Send hover move after UP to tell the application that the mouse is hovering now. michael@0: if (motionEventAction == AMOTION_EVENT_ACTION_UP michael@0: && mPointerController != NULL) { michael@0: NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags, michael@0: AMOTION_EVENT_ACTION_HOVER_MOVE, 0, michael@0: metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE, michael@0: displayId, 1, &pointerProperties, &pointerCoords, michael@0: mXPrecision, mYPrecision, downTime); michael@0: getListener()->notifyMotion(&hoverArgs); michael@0: } michael@0: michael@0: // Send scroll events. michael@0: if (scrolled) { michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); michael@0: michael@0: NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags, michael@0: AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState, michael@0: AMOTION_EVENT_EDGE_FLAG_NONE, michael@0: displayId, 1, &pointerProperties, &pointerCoords, michael@0: mXPrecision, mYPrecision, downTime); michael@0: getListener()->notifyMotion(&scrollArgs); michael@0: } michael@0: } michael@0: michael@0: // Synthesize key up from buttons if needed. michael@0: synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, michael@0: policyFlags, lastButtonState, currentButtonState); michael@0: michael@0: mCursorMotionAccumulator.finishSync(); michael@0: mCursorScrollAccumulator.finishSync(); michael@0: } michael@0: michael@0: int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { michael@0: if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { michael@0: return getEventHub()->getScanCodeState(getDeviceId(), scanCode); michael@0: } else { michael@0: return AKEY_STATE_UNKNOWN; michael@0: } michael@0: } michael@0: michael@0: void CursorInputMapper::fadePointer() { michael@0: if (mPointerController != NULL) { michael@0: mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); michael@0: } michael@0: } michael@0: michael@0: michael@0: // --- TouchInputMapper --- michael@0: michael@0: TouchInputMapper::TouchInputMapper(InputDevice* device) : michael@0: InputMapper(device), michael@0: mSource(0), mDeviceMode(DEVICE_MODE_DISABLED), michael@0: mSurfaceWidth(-1), mSurfaceHeight(-1), mSurfaceLeft(0), mSurfaceTop(0), michael@0: mSurfaceOrientation(DISPLAY_ORIENTATION_0) { michael@0: } michael@0: michael@0: TouchInputMapper::~TouchInputMapper() { michael@0: } michael@0: michael@0: uint32_t TouchInputMapper::getSources() { michael@0: return mSource; michael@0: } michael@0: michael@0: void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { michael@0: InputMapper::populateDeviceInfo(info); michael@0: michael@0: if (mDeviceMode != DEVICE_MODE_DISABLED) { michael@0: info->addMotionRange(mOrientedRanges.x); michael@0: info->addMotionRange(mOrientedRanges.y); michael@0: info->addMotionRange(mOrientedRanges.pressure); michael@0: michael@0: if (mOrientedRanges.haveSize) { michael@0: info->addMotionRange(mOrientedRanges.size); michael@0: } michael@0: michael@0: if (mOrientedRanges.haveTouchSize) { michael@0: info->addMotionRange(mOrientedRanges.touchMajor); michael@0: info->addMotionRange(mOrientedRanges.touchMinor); michael@0: } michael@0: michael@0: if (mOrientedRanges.haveToolSize) { michael@0: info->addMotionRange(mOrientedRanges.toolMajor); michael@0: info->addMotionRange(mOrientedRanges.toolMinor); michael@0: } michael@0: michael@0: if (mOrientedRanges.haveOrientation) { michael@0: info->addMotionRange(mOrientedRanges.orientation); michael@0: } michael@0: michael@0: if (mOrientedRanges.haveDistance) { michael@0: info->addMotionRange(mOrientedRanges.distance); michael@0: } michael@0: michael@0: if (mOrientedRanges.haveTilt) { michael@0: info->addMotionRange(mOrientedRanges.tilt); michael@0: } michael@0: michael@0: if (mCursorScrollAccumulator.haveRelativeVWheel()) { michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, michael@0: 0.0f); michael@0: } michael@0: if (mCursorScrollAccumulator.haveRelativeHWheel()) { michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, michael@0: 0.0f); michael@0: } michael@0: if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) { michael@0: const InputDeviceInfo::MotionRange& x = mOrientedRanges.x; michael@0: const InputDeviceInfo::MotionRange& y = mOrientedRanges.y; michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat, michael@0: x.fuzz, x.resolution); michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat, michael@0: y.fuzz, y.resolution); michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat, michael@0: x.fuzz, x.resolution); michael@0: info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat, michael@0: y.fuzz, y.resolution); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::dump(String8& dump) { michael@0: dump.append(INDENT2 "Touch Input Mapper:\n"); michael@0: dumpParameters(dump); michael@0: dumpVirtualKeys(dump); michael@0: dumpRawPointerAxes(dump); michael@0: dumpCalibration(dump); michael@0: dumpSurface(dump); michael@0: michael@0: dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); michael@0: dump.appendFormat(INDENT4 "XTranslate: %0.3f\n", mXTranslate); michael@0: dump.appendFormat(INDENT4 "YTranslate: %0.3f\n", mYTranslate); michael@0: dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale); michael@0: dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale); michael@0: dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision); michael@0: dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision); michael@0: dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale); michael@0: dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale); michael@0: dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale); michael@0: dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale); michael@0: dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale); michael@0: dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt)); michael@0: dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter); michael@0: dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale); michael@0: dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter); michael@0: dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale); michael@0: michael@0: dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState); michael@0: michael@0: dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n", michael@0: mLastRawPointerData.pointerCount); michael@0: for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) { michael@0: const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i]; michael@0: dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " michael@0: "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " michael@0: "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, " michael@0: "toolType=%d, isHovering=%s\n", i, michael@0: pointer.id, pointer.x, pointer.y, pointer.pressure, michael@0: pointer.touchMajor, pointer.touchMinor, michael@0: pointer.toolMajor, pointer.toolMinor, michael@0: pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance, michael@0: pointer.toolType, toString(pointer.isHovering)); michael@0: } michael@0: michael@0: dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n", michael@0: mLastCookedPointerData.pointerCount); michael@0: for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) { michael@0: const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i]; michael@0: const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i]; michael@0: dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, " michael@0: "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, " michael@0: "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, " michael@0: "toolType=%d, isHovering=%s\n", i, michael@0: pointerProperties.id, michael@0: pointerCoords.getX(), michael@0: pointerCoords.getY(), michael@0: pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), michael@0: pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), michael@0: pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), michael@0: pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), michael@0: pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), michael@0: pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), michael@0: pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT), michael@0: pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), michael@0: pointerProperties.toolType, michael@0: toString(mLastCookedPointerData.isHovering(i))); michael@0: } michael@0: michael@0: if (mDeviceMode == DEVICE_MODE_POINTER) { michael@0: dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n"); michael@0: dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n", michael@0: mPointerXMovementScale); michael@0: dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n", michael@0: mPointerYMovementScale); michael@0: dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n", michael@0: mPointerXZoomScale); michael@0: dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n", michael@0: mPointerYZoomScale); michael@0: dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n", michael@0: mPointerGestureMaxSwipeWidth); michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::configure(nsecs_t when, michael@0: const InputReaderConfiguration* config, uint32_t changes) { michael@0: InputMapper::configure(when, config, changes); michael@0: michael@0: mConfig = *config; michael@0: michael@0: if (!changes) { // first time only michael@0: // Configure basic parameters. michael@0: configureParameters(); michael@0: michael@0: // Configure common accumulators. michael@0: mCursorScrollAccumulator.configure(getDevice()); michael@0: mTouchButtonAccumulator.configure(getDevice()); michael@0: michael@0: // Configure absolute axis information. michael@0: configureRawPointerAxes(); michael@0: michael@0: // Prepare input device calibration. michael@0: parseCalibration(); michael@0: resolveCalibration(); michael@0: } michael@0: michael@0: if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { michael@0: // Update pointer speed. michael@0: mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters); michael@0: mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); michael@0: mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); michael@0: } michael@0: michael@0: bool resetNeeded = false; michael@0: if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO michael@0: | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT michael@0: | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) { michael@0: // Configure device sources, surface dimensions, orientation and michael@0: // scaling factors. michael@0: configureSurface(when, &resetNeeded); michael@0: } michael@0: michael@0: if (changes && resetNeeded) { michael@0: // Send reset, unless this is the first time the device has been configured, michael@0: // in which case the reader will call reset itself after all mappers are ready. michael@0: getDevice()->notifyReset(when); michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::configureParameters() { michael@0: // Use the pointer presentation mode for devices that do not support distinct michael@0: // multitouch. The spot-based presentation relies on being able to accurately michael@0: // locate two or more fingers on the touch pad. michael@0: mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT) michael@0: ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS; michael@0: michael@0: String8 gestureModeString; michael@0: if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), michael@0: gestureModeString)) { michael@0: if (gestureModeString == "pointer") { michael@0: mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER; michael@0: } else if (gestureModeString == "spots") { michael@0: mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; michael@0: } else if (gestureModeString != "default") { michael@0: ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); michael@0: } michael@0: } michael@0: michael@0: if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) { michael@0: // The device is a touch screen. michael@0: mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; michael@0: } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) { michael@0: // The device is a pointing device like a track pad. michael@0: mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; michael@0: } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X) michael@0: || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { michael@0: // The device is a cursor device with a touch pad attached. michael@0: // By default don't use the touch pad to move the pointer. michael@0: mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; michael@0: } else { michael@0: // The device is a touch pad of unknown purpose. michael@0: mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; michael@0: } michael@0: michael@0: String8 deviceTypeString; michael@0: if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), michael@0: deviceTypeString)) { michael@0: if (deviceTypeString == "touchScreen") { michael@0: mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; michael@0: } else if (deviceTypeString == "touchPad") { michael@0: mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; michael@0: } else if (deviceTypeString == "touchNavigation") { michael@0: mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_NAVIGATION; michael@0: } else if (deviceTypeString == "pointer") { michael@0: mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; michael@0: } else if (deviceTypeString != "default") { michael@0: ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); michael@0: } michael@0: } michael@0: michael@0: mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; michael@0: getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), michael@0: mParameters.orientationAware); michael@0: michael@0: mParameters.hasAssociatedDisplay = false; michael@0: mParameters.associatedDisplayIsExternal = false; michael@0: if (mParameters.orientationAware michael@0: || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN michael@0: || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { michael@0: mParameters.hasAssociatedDisplay = true; michael@0: mParameters.associatedDisplayIsExternal = michael@0: mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN michael@0: && getDevice()->isExternal(); michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::dumpParameters(String8& dump) { michael@0: dump.append(INDENT3 "Parameters:\n"); michael@0: michael@0: switch (mParameters.gestureMode) { michael@0: case Parameters::GESTURE_MODE_POINTER: michael@0: dump.append(INDENT4 "GestureMode: pointer\n"); michael@0: break; michael@0: case Parameters::GESTURE_MODE_SPOTS: michael@0: dump.append(INDENT4 "GestureMode: spots\n"); michael@0: break; michael@0: default: michael@0: assert(false); michael@0: } michael@0: michael@0: switch (mParameters.deviceType) { michael@0: case Parameters::DEVICE_TYPE_TOUCH_SCREEN: michael@0: dump.append(INDENT4 "DeviceType: touchScreen\n"); michael@0: break; michael@0: case Parameters::DEVICE_TYPE_TOUCH_PAD: michael@0: dump.append(INDENT4 "DeviceType: touchPad\n"); michael@0: break; michael@0: case Parameters::DEVICE_TYPE_TOUCH_NAVIGATION: michael@0: dump.append(INDENT4 "DeviceType: touchNavigation\n"); michael@0: break; michael@0: case Parameters::DEVICE_TYPE_POINTER: michael@0: dump.append(INDENT4 "DeviceType: pointer\n"); michael@0: break; michael@0: default: michael@0: ALOG_ASSERT(false); michael@0: } michael@0: michael@0: dump.appendFormat(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s\n", michael@0: toString(mParameters.hasAssociatedDisplay), michael@0: toString(mParameters.associatedDisplayIsExternal)); michael@0: dump.appendFormat(INDENT4 "OrientationAware: %s\n", michael@0: toString(mParameters.orientationAware)); michael@0: } michael@0: michael@0: void TouchInputMapper::configureRawPointerAxes() { michael@0: mRawPointerAxes.clear(); michael@0: } michael@0: michael@0: void TouchInputMapper::dumpRawPointerAxes(String8& dump) { michael@0: dump.append(INDENT3 "Raw Touch Axes:\n"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId"); michael@0: dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot"); michael@0: } michael@0: michael@0: void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) { michael@0: int32_t oldDeviceMode = mDeviceMode; michael@0: michael@0: // Determine device mode. michael@0: if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER michael@0: && mConfig.pointerGesturesEnabled) { michael@0: mSource = AINPUT_SOURCE_MOUSE; michael@0: mDeviceMode = DEVICE_MODE_POINTER; michael@0: if (hasStylus()) { michael@0: mSource |= AINPUT_SOURCE_STYLUS; michael@0: } michael@0: } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN michael@0: && mParameters.hasAssociatedDisplay) { michael@0: mSource = AINPUT_SOURCE_TOUCHSCREEN; michael@0: mDeviceMode = DEVICE_MODE_DIRECT; michael@0: if (hasStylus()) { michael@0: mSource |= AINPUT_SOURCE_STYLUS; michael@0: } michael@0: } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_NAVIGATION) { michael@0: mSource = AINPUT_SOURCE_TOUCH_NAVIGATION; michael@0: mDeviceMode = DEVICE_MODE_NAVIGATION; michael@0: } else { michael@0: mSource = AINPUT_SOURCE_TOUCHPAD; michael@0: mDeviceMode = DEVICE_MODE_UNSCALED; michael@0: } michael@0: michael@0: // Ensure we have valid X and Y axes. michael@0: if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) { michael@0: ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! " michael@0: "The device will be inoperable.", getDeviceName().string()); michael@0: mDeviceMode = DEVICE_MODE_DISABLED; michael@0: return; michael@0: } michael@0: michael@0: // Raw width and height in the natural orientation. michael@0: int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; michael@0: int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; michael@0: michael@0: // Get associated display dimensions. michael@0: bool viewportChanged = false; michael@0: DisplayViewport newViewport; michael@0: if (mParameters.hasAssociatedDisplay) { michael@0: if (!mConfig.getDisplayInfo(mParameters.associatedDisplayIsExternal, &newViewport)) { michael@0: ALOGI(INDENT "Touch device '%s' could not query the properties of its associated " michael@0: "display. The device will be inoperable until the display size " michael@0: "becomes available.", michael@0: getDeviceName().string()); michael@0: mDeviceMode = DEVICE_MODE_DISABLED; michael@0: return; michael@0: } michael@0: } else { michael@0: newViewport.setNonDisplayViewport(rawWidth, rawHeight); michael@0: } michael@0: if (mViewport != newViewport) { michael@0: mViewport = newViewport; michael@0: viewportChanged = true; michael@0: michael@0: if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) { michael@0: // Convert rotated viewport to natural surface coordinates. michael@0: int32_t naturalLogicalWidth, naturalLogicalHeight; michael@0: int32_t naturalPhysicalWidth, naturalPhysicalHeight; michael@0: int32_t naturalPhysicalLeft, naturalPhysicalTop; michael@0: int32_t naturalDeviceWidth, naturalDeviceHeight; michael@0: switch (mViewport.orientation) { michael@0: case DISPLAY_ORIENTATION_90: michael@0: naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop; michael@0: naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft; michael@0: naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop; michael@0: naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft; michael@0: naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom; michael@0: naturalPhysicalTop = mViewport.physicalLeft; michael@0: naturalDeviceWidth = mViewport.deviceHeight; michael@0: naturalDeviceHeight = mViewport.deviceWidth; michael@0: break; michael@0: case DISPLAY_ORIENTATION_180: michael@0: naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft; michael@0: naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop; michael@0: naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft; michael@0: naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop; michael@0: naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight; michael@0: naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom; michael@0: naturalDeviceWidth = mViewport.deviceWidth; michael@0: naturalDeviceHeight = mViewport.deviceHeight; michael@0: break; michael@0: case DISPLAY_ORIENTATION_270: michael@0: naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop; michael@0: naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft; michael@0: naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop; michael@0: naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft; michael@0: naturalPhysicalLeft = mViewport.physicalTop; michael@0: naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight; michael@0: naturalDeviceWidth = mViewport.deviceHeight; michael@0: naturalDeviceHeight = mViewport.deviceWidth; michael@0: break; michael@0: case DISPLAY_ORIENTATION_0: michael@0: default: michael@0: naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft; michael@0: naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop; michael@0: naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft; michael@0: naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop; michael@0: naturalPhysicalLeft = mViewport.physicalLeft; michael@0: naturalPhysicalTop = mViewport.physicalTop; michael@0: naturalDeviceWidth = mViewport.deviceWidth; michael@0: naturalDeviceHeight = mViewport.deviceHeight; michael@0: break; michael@0: } michael@0: michael@0: mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth; michael@0: mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight; michael@0: mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth; michael@0: mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight; michael@0: michael@0: mSurfaceOrientation = mParameters.orientationAware ? michael@0: mViewport.orientation : DISPLAY_ORIENTATION_0; michael@0: } else { michael@0: mSurfaceWidth = rawWidth; michael@0: mSurfaceHeight = rawHeight; michael@0: mSurfaceLeft = 0; michael@0: mSurfaceTop = 0; michael@0: mSurfaceOrientation = DISPLAY_ORIENTATION_0; michael@0: } michael@0: } michael@0: michael@0: // If moving between pointer modes, need to reset some state. michael@0: bool deviceModeChanged; michael@0: if (mDeviceMode != oldDeviceMode) { michael@0: deviceModeChanged = true; michael@0: mOrientedRanges.clear(); michael@0: } michael@0: michael@0: // Create pointer controller if needed. michael@0: if (mDeviceMode == DEVICE_MODE_POINTER || michael@0: (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) { michael@0: if (mPointerController == NULL) { michael@0: mPointerController = getPolicy()->obtainPointerController(getDeviceId()); michael@0: } michael@0: } else { michael@0: mPointerController.clear(); michael@0: } michael@0: michael@0: if (viewportChanged || deviceModeChanged) { michael@0: ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, " michael@0: "display id %d", michael@0: getDeviceId(), getDeviceName().string(), mSurfaceWidth, mSurfaceHeight, michael@0: mSurfaceOrientation, mDeviceMode, mViewport.displayId); michael@0: michael@0: // Configure X and Y factors. michael@0: mXScale = float(mSurfaceWidth) / rawWidth; michael@0: mYScale = float(mSurfaceHeight) / rawHeight; michael@0: mXTranslate = -mSurfaceLeft; michael@0: mYTranslate = -mSurfaceTop; michael@0: mXPrecision = 1.0f / mXScale; michael@0: mYPrecision = 1.0f / mYScale; michael@0: michael@0: mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X; michael@0: mOrientedRanges.x.source = mSource; michael@0: mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; michael@0: mOrientedRanges.y.source = mSource; michael@0: michael@0: configureVirtualKeys(); michael@0: michael@0: // Scale factor for terms that are not oriented in a particular axis. michael@0: // If the pixels are square then xScale == yScale otherwise we fake it michael@0: // by choosing an average. michael@0: mGeometricScale = avg(mXScale, mYScale); michael@0: michael@0: // Size of diagonal axis. michael@0: float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight); michael@0: michael@0: // Size factors. michael@0: if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { michael@0: if (mRawPointerAxes.touchMajor.valid michael@0: && mRawPointerAxes.touchMajor.maxValue != 0) { michael@0: mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue; michael@0: } else if (mRawPointerAxes.toolMajor.valid michael@0: && mRawPointerAxes.toolMajor.maxValue != 0) { michael@0: mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue; michael@0: } else { michael@0: mSizeScale = 0.0f; michael@0: } michael@0: michael@0: mOrientedRanges.haveTouchSize = true; michael@0: mOrientedRanges.haveToolSize = true; michael@0: mOrientedRanges.haveSize = true; michael@0: michael@0: mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; michael@0: mOrientedRanges.touchMajor.source = mSource; michael@0: mOrientedRanges.touchMajor.min = 0; michael@0: mOrientedRanges.touchMajor.max = diagonalSize; michael@0: mOrientedRanges.touchMajor.flat = 0; michael@0: mOrientedRanges.touchMajor.fuzz = 0; michael@0: mOrientedRanges.touchMajor.resolution = 0; michael@0: michael@0: mOrientedRanges.touchMinor = mOrientedRanges.touchMajor; michael@0: mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; michael@0: michael@0: mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; michael@0: mOrientedRanges.toolMajor.source = mSource; michael@0: mOrientedRanges.toolMajor.min = 0; michael@0: mOrientedRanges.toolMajor.max = diagonalSize; michael@0: mOrientedRanges.toolMajor.flat = 0; michael@0: mOrientedRanges.toolMajor.fuzz = 0; michael@0: mOrientedRanges.toolMajor.resolution = 0; michael@0: michael@0: mOrientedRanges.toolMinor = mOrientedRanges.toolMajor; michael@0: mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; michael@0: michael@0: mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; michael@0: mOrientedRanges.size.source = mSource; michael@0: mOrientedRanges.size.min = 0; michael@0: mOrientedRanges.size.max = 1.0; michael@0: mOrientedRanges.size.flat = 0; michael@0: mOrientedRanges.size.fuzz = 0; michael@0: mOrientedRanges.size.resolution = 0; michael@0: } else { michael@0: mSizeScale = 0.0f; michael@0: } michael@0: michael@0: // Pressure factors. michael@0: mPressureScale = 0; michael@0: if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL michael@0: || mCalibration.pressureCalibration michael@0: == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { michael@0: if (mCalibration.havePressureScale) { michael@0: mPressureScale = mCalibration.pressureScale; michael@0: } else if (mRawPointerAxes.pressure.valid michael@0: && mRawPointerAxes.pressure.maxValue != 0) { michael@0: mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue; michael@0: } michael@0: } michael@0: michael@0: mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; michael@0: mOrientedRanges.pressure.source = mSource; michael@0: mOrientedRanges.pressure.min = 0; michael@0: mOrientedRanges.pressure.max = 1.0; michael@0: mOrientedRanges.pressure.flat = 0; michael@0: mOrientedRanges.pressure.fuzz = 0; michael@0: mOrientedRanges.pressure.resolution = 0; michael@0: michael@0: // Tilt michael@0: mTiltXCenter = 0; michael@0: mTiltXScale = 0; michael@0: mTiltYCenter = 0; michael@0: mTiltYScale = 0; michael@0: mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid; michael@0: if (mHaveTilt) { michael@0: mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, michael@0: mRawPointerAxes.tiltX.maxValue); michael@0: mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, michael@0: mRawPointerAxes.tiltY.maxValue); michael@0: mTiltXScale = M_PI / 180; michael@0: mTiltYScale = M_PI / 180; michael@0: michael@0: mOrientedRanges.haveTilt = true; michael@0: michael@0: mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT; michael@0: mOrientedRanges.tilt.source = mSource; michael@0: mOrientedRanges.tilt.min = 0; michael@0: mOrientedRanges.tilt.max = M_PI_2; michael@0: mOrientedRanges.tilt.flat = 0; michael@0: mOrientedRanges.tilt.fuzz = 0; michael@0: mOrientedRanges.tilt.resolution = 0; michael@0: } michael@0: michael@0: // Orientation michael@0: mOrientationScale = 0; michael@0: if (mHaveTilt) { michael@0: mOrientedRanges.haveOrientation = true; michael@0: michael@0: mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; michael@0: mOrientedRanges.orientation.source = mSource; michael@0: mOrientedRanges.orientation.min = -M_PI; michael@0: mOrientedRanges.orientation.max = M_PI; michael@0: mOrientedRanges.orientation.flat = 0; michael@0: mOrientedRanges.orientation.fuzz = 0; michael@0: mOrientedRanges.orientation.resolution = 0; michael@0: } else if (mCalibration.orientationCalibration != michael@0: Calibration::ORIENTATION_CALIBRATION_NONE) { michael@0: if (mCalibration.orientationCalibration michael@0: == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { michael@0: if (mRawPointerAxes.orientation.valid) { michael@0: if (mRawPointerAxes.orientation.maxValue > 0) { michael@0: mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue; michael@0: } else if (mRawPointerAxes.orientation.minValue < 0) { michael@0: mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue; michael@0: } else { michael@0: mOrientationScale = 0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: mOrientedRanges.haveOrientation = true; michael@0: michael@0: mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; michael@0: mOrientedRanges.orientation.source = mSource; michael@0: mOrientedRanges.orientation.min = -M_PI_2; michael@0: mOrientedRanges.orientation.max = M_PI_2; michael@0: mOrientedRanges.orientation.flat = 0; michael@0: mOrientedRanges.orientation.fuzz = 0; michael@0: mOrientedRanges.orientation.resolution = 0; michael@0: } michael@0: michael@0: // Distance michael@0: mDistanceScale = 0; michael@0: if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) { michael@0: if (mCalibration.distanceCalibration michael@0: == Calibration::DISTANCE_CALIBRATION_SCALED) { michael@0: if (mCalibration.haveDistanceScale) { michael@0: mDistanceScale = mCalibration.distanceScale; michael@0: } else { michael@0: mDistanceScale = 1.0f; michael@0: } michael@0: } michael@0: michael@0: mOrientedRanges.haveDistance = true; michael@0: michael@0: mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE; michael@0: mOrientedRanges.distance.source = mSource; michael@0: mOrientedRanges.distance.min = michael@0: mRawPointerAxes.distance.minValue * mDistanceScale; michael@0: mOrientedRanges.distance.max = michael@0: mRawPointerAxes.distance.maxValue * mDistanceScale; michael@0: mOrientedRanges.distance.flat = 0; michael@0: mOrientedRanges.distance.fuzz = michael@0: mRawPointerAxes.distance.fuzz * mDistanceScale; michael@0: mOrientedRanges.distance.resolution = 0; michael@0: } michael@0: michael@0: // Compute oriented precision, scales and ranges. michael@0: // Note that the maximum value reported is an inclusive maximum value so it is one michael@0: // unit less than the total width or height of surface. michael@0: switch (mSurfaceOrientation) { michael@0: case DISPLAY_ORIENTATION_90: michael@0: case DISPLAY_ORIENTATION_270: michael@0: mOrientedXPrecision = mYPrecision; michael@0: mOrientedYPrecision = mXPrecision; michael@0: michael@0: mOrientedRanges.x.min = mYTranslate; michael@0: mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1; michael@0: mOrientedRanges.x.flat = 0; michael@0: mOrientedRanges.x.fuzz = 0; michael@0: mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale; michael@0: michael@0: mOrientedRanges.y.min = mXTranslate; michael@0: mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1; michael@0: mOrientedRanges.y.flat = 0; michael@0: mOrientedRanges.y.fuzz = 0; michael@0: mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale; michael@0: break; michael@0: michael@0: default: michael@0: mOrientedXPrecision = mXPrecision; michael@0: mOrientedYPrecision = mYPrecision; michael@0: michael@0: mOrientedRanges.x.min = mXTranslate; michael@0: mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1; michael@0: mOrientedRanges.x.flat = 0; michael@0: mOrientedRanges.x.fuzz = 0; michael@0: mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale; michael@0: michael@0: mOrientedRanges.y.min = mYTranslate; michael@0: mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1; michael@0: mOrientedRanges.y.flat = 0; michael@0: mOrientedRanges.y.fuzz = 0; michael@0: mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale; michael@0: break; michael@0: } michael@0: michael@0: if (mDeviceMode == DEVICE_MODE_POINTER) { michael@0: // Compute pointer gesture detection parameters. michael@0: float rawDiagonal = hypotf(rawWidth, rawHeight); michael@0: float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight); michael@0: michael@0: // Scale movements such that one whole swipe of the touch pad covers a michael@0: // given area relative to the diagonal size of the display when no acceleration michael@0: // is applied. michael@0: // Assume that the touch pad has a square aspect ratio such that movements in michael@0: // X and Y of the same number of raw units cover the same physical distance. michael@0: mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio michael@0: * displayDiagonal / rawDiagonal; michael@0: mPointerYMovementScale = mPointerXMovementScale; michael@0: michael@0: // Scale zooms to cover a smaller range of the display than movements do. michael@0: // This value determines the area around the pointer that is affected by freeform michael@0: // pointer gestures. michael@0: mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio michael@0: * displayDiagonal / rawDiagonal; michael@0: mPointerYZoomScale = mPointerXZoomScale; michael@0: michael@0: // Max width between pointers to detect a swipe gesture is more than some fraction michael@0: // of the diagonal axis of the touch pad. Touches that are wider than this are michael@0: // translated into freeform gestures. michael@0: mPointerGestureMaxSwipeWidth = michael@0: mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal; michael@0: michael@0: // Abort current pointer usages because the state has changed. michael@0: abortPointerUsage(when, 0 /*policyFlags*/); michael@0: } michael@0: michael@0: // Inform the dispatcher about the changes. michael@0: *outResetNeeded = true; michael@0: bumpGeneration(); michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::dumpSurface(String8& dump) { michael@0: dump.appendFormat(INDENT3 "Viewport: displayId=%d, orientation=%d, " michael@0: "logicalFrame=[%d, %d, %d, %d], " michael@0: "physicalFrame=[%d, %d, %d, %d], " michael@0: "deviceSize=[%d, %d]\n", michael@0: mViewport.displayId, mViewport.orientation, michael@0: mViewport.logicalLeft, mViewport.logicalTop, michael@0: mViewport.logicalRight, mViewport.logicalBottom, michael@0: mViewport.physicalLeft, mViewport.physicalTop, michael@0: mViewport.physicalRight, mViewport.physicalBottom, michael@0: mViewport.deviceWidth, mViewport.deviceHeight); michael@0: michael@0: dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth); michael@0: dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight); michael@0: dump.appendFormat(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft); michael@0: dump.appendFormat(INDENT3 "SurfaceTop: %d\n", mSurfaceTop); michael@0: dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation); michael@0: } michael@0: michael@0: void TouchInputMapper::configureVirtualKeys() { michael@0: Vector virtualKeyDefinitions; michael@0: getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); michael@0: michael@0: mVirtualKeys.clear(); michael@0: michael@0: if (virtualKeyDefinitions.size() == 0) { michael@0: return; michael@0: } michael@0: michael@0: mVirtualKeys.setCapacity(virtualKeyDefinitions.size()); michael@0: michael@0: int32_t touchScreenLeft = mRawPointerAxes.x.minValue; michael@0: int32_t touchScreenTop = mRawPointerAxes.y.minValue; michael@0: int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; michael@0: int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; michael@0: michael@0: for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { michael@0: const VirtualKeyDefinition& virtualKeyDefinition = michael@0: virtualKeyDefinitions[i]; michael@0: michael@0: mVirtualKeys.add(); michael@0: VirtualKey& virtualKey = mVirtualKeys.editTop(); michael@0: michael@0: virtualKey.scanCode = virtualKeyDefinition.scanCode; michael@0: int32_t keyCode; michael@0: uint32_t flags; michael@0: if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) { michael@0: ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", michael@0: virtualKey.scanCode); michael@0: mVirtualKeys.pop(); // drop the key michael@0: continue; michael@0: } michael@0: michael@0: virtualKey.keyCode = keyCode; michael@0: virtualKey.flags = flags; michael@0: michael@0: // convert the key definition's display coordinates into touch coordinates for a hit box michael@0: int32_t halfWidth = virtualKeyDefinition.width / 2; michael@0: int32_t halfHeight = virtualKeyDefinition.height / 2; michael@0: michael@0: virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) michael@0: * touchScreenWidth / mSurfaceWidth + touchScreenLeft; michael@0: virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) michael@0: * touchScreenWidth / mSurfaceWidth + touchScreenLeft; michael@0: virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) michael@0: * touchScreenHeight / mSurfaceHeight + touchScreenTop; michael@0: virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) michael@0: * touchScreenHeight / mSurfaceHeight + touchScreenTop; michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::dumpVirtualKeys(String8& dump) { michael@0: if (!mVirtualKeys.isEmpty()) { michael@0: dump.append(INDENT3 "Virtual Keys:\n"); michael@0: michael@0: for (size_t i = 0; i < mVirtualKeys.size(); i++) { michael@0: const VirtualKey& virtualKey = mVirtualKeys.itemAt(i); michael@0: dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " michael@0: "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", michael@0: i, virtualKey.scanCode, virtualKey.keyCode, michael@0: virtualKey.hitLeft, virtualKey.hitRight, michael@0: virtualKey.hitTop, virtualKey.hitBottom); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::parseCalibration() { michael@0: const PropertyMap& in = getDevice()->getConfiguration(); michael@0: Calibration& out = mCalibration; michael@0: michael@0: // Size michael@0: out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; michael@0: String8 sizeCalibrationString; michael@0: if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { michael@0: if (sizeCalibrationString == "none") { michael@0: out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; michael@0: } else if (sizeCalibrationString == "geometric") { michael@0: out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; michael@0: } else if (sizeCalibrationString == "diameter") { michael@0: out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER; michael@0: } else if (sizeCalibrationString == "box") { michael@0: out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX; michael@0: } else if (sizeCalibrationString == "area") { michael@0: out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA; michael@0: } else if (sizeCalibrationString != "default") { michael@0: ALOGW("Invalid value for touch.size.calibration: '%s'", michael@0: sizeCalibrationString.string()); michael@0: } michael@0: } michael@0: michael@0: out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), michael@0: out.sizeScale); michael@0: out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), michael@0: out.sizeBias); michael@0: out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), michael@0: out.sizeIsSummed); michael@0: michael@0: // Pressure michael@0: out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; michael@0: String8 pressureCalibrationString; michael@0: if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { michael@0: if (pressureCalibrationString == "none") { michael@0: out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; michael@0: } else if (pressureCalibrationString == "physical") { michael@0: out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; michael@0: } else if (pressureCalibrationString == "amplitude") { michael@0: out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; michael@0: } else if (pressureCalibrationString != "default") { michael@0: ALOGW("Invalid value for touch.pressure.calibration: '%s'", michael@0: pressureCalibrationString.string()); michael@0: } michael@0: } michael@0: michael@0: out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), michael@0: out.pressureScale); michael@0: michael@0: // Orientation michael@0: out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; michael@0: String8 orientationCalibrationString; michael@0: if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { michael@0: if (orientationCalibrationString == "none") { michael@0: out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; michael@0: } else if (orientationCalibrationString == "interpolated") { michael@0: out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; michael@0: } else if (orientationCalibrationString == "vector") { michael@0: out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; michael@0: } else if (orientationCalibrationString != "default") { michael@0: ALOGW("Invalid value for touch.orientation.calibration: '%s'", michael@0: orientationCalibrationString.string()); michael@0: } michael@0: } michael@0: michael@0: // Distance michael@0: out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT; michael@0: String8 distanceCalibrationString; michael@0: if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) { michael@0: if (distanceCalibrationString == "none") { michael@0: out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; michael@0: } else if (distanceCalibrationString == "scaled") { michael@0: out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; michael@0: } else if (distanceCalibrationString != "default") { michael@0: ALOGW("Invalid value for touch.distance.calibration: '%s'", michael@0: distanceCalibrationString.string()); michael@0: } michael@0: } michael@0: michael@0: out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), michael@0: out.distanceScale); michael@0: michael@0: out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_DEFAULT; michael@0: String8 coverageCalibrationString; michael@0: if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) { michael@0: if (coverageCalibrationString == "none") { michael@0: out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE; michael@0: } else if (coverageCalibrationString == "box") { michael@0: out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_BOX; michael@0: } else if (coverageCalibrationString != "default") { michael@0: ALOGW("Invalid value for touch.coverage.calibration: '%s'", michael@0: coverageCalibrationString.string()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::resolveCalibration() { michael@0: // Size michael@0: if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) { michael@0: if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) { michael@0: mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; michael@0: } michael@0: } else { michael@0: mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; michael@0: } michael@0: michael@0: // Pressure michael@0: if (mRawPointerAxes.pressure.valid) { michael@0: if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) { michael@0: mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; michael@0: } michael@0: } else { michael@0: mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; michael@0: } michael@0: michael@0: // Orientation michael@0: if (mRawPointerAxes.orientation.valid) { michael@0: if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) { michael@0: mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; michael@0: } michael@0: } else { michael@0: mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; michael@0: } michael@0: michael@0: // Distance michael@0: if (mRawPointerAxes.distance.valid) { michael@0: if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) { michael@0: mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; michael@0: } michael@0: } else { michael@0: mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; michael@0: } michael@0: michael@0: // Coverage michael@0: if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_DEFAULT) { michael@0: mCalibration.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE; michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::dumpCalibration(String8& dump) { michael@0: dump.append(INDENT3 "Calibration:\n"); michael@0: michael@0: // Size michael@0: switch (mCalibration.sizeCalibration) { michael@0: case Calibration::SIZE_CALIBRATION_NONE: michael@0: dump.append(INDENT4 "touch.size.calibration: none\n"); michael@0: break; michael@0: case Calibration::SIZE_CALIBRATION_GEOMETRIC: michael@0: dump.append(INDENT4 "touch.size.calibration: geometric\n"); michael@0: break; michael@0: case Calibration::SIZE_CALIBRATION_DIAMETER: michael@0: dump.append(INDENT4 "touch.size.calibration: diameter\n"); michael@0: break; michael@0: case Calibration::SIZE_CALIBRATION_BOX: michael@0: dump.append(INDENT4 "touch.size.calibration: box\n"); michael@0: break; michael@0: case Calibration::SIZE_CALIBRATION_AREA: michael@0: dump.append(INDENT4 "touch.size.calibration: area\n"); michael@0: break; michael@0: default: michael@0: ALOG_ASSERT(false); michael@0: } michael@0: michael@0: if (mCalibration.haveSizeScale) { michael@0: dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n", michael@0: mCalibration.sizeScale); michael@0: } michael@0: michael@0: if (mCalibration.haveSizeBias) { michael@0: dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n", michael@0: mCalibration.sizeBias); michael@0: } michael@0: michael@0: if (mCalibration.haveSizeIsSummed) { michael@0: dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n", michael@0: toString(mCalibration.sizeIsSummed)); michael@0: } michael@0: michael@0: // Pressure michael@0: switch (mCalibration.pressureCalibration) { michael@0: case Calibration::PRESSURE_CALIBRATION_NONE: michael@0: dump.append(INDENT4 "touch.pressure.calibration: none\n"); michael@0: break; michael@0: case Calibration::PRESSURE_CALIBRATION_PHYSICAL: michael@0: dump.append(INDENT4 "touch.pressure.calibration: physical\n"); michael@0: break; michael@0: case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: michael@0: dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); michael@0: break; michael@0: default: michael@0: ALOG_ASSERT(false); michael@0: } michael@0: michael@0: if (mCalibration.havePressureScale) { michael@0: dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", michael@0: mCalibration.pressureScale); michael@0: } michael@0: michael@0: // Orientation michael@0: switch (mCalibration.orientationCalibration) { michael@0: case Calibration::ORIENTATION_CALIBRATION_NONE: michael@0: dump.append(INDENT4 "touch.orientation.calibration: none\n"); michael@0: break; michael@0: case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: michael@0: dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); michael@0: break; michael@0: case Calibration::ORIENTATION_CALIBRATION_VECTOR: michael@0: dump.append(INDENT4 "touch.orientation.calibration: vector\n"); michael@0: break; michael@0: default: michael@0: ALOG_ASSERT(false); michael@0: } michael@0: michael@0: // Distance michael@0: switch (mCalibration.distanceCalibration) { michael@0: case Calibration::DISTANCE_CALIBRATION_NONE: michael@0: dump.append(INDENT4 "touch.distance.calibration: none\n"); michael@0: break; michael@0: case Calibration::DISTANCE_CALIBRATION_SCALED: michael@0: dump.append(INDENT4 "touch.distance.calibration: scaled\n"); michael@0: break; michael@0: default: michael@0: ALOG_ASSERT(false); michael@0: } michael@0: michael@0: if (mCalibration.haveDistanceScale) { michael@0: dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n", michael@0: mCalibration.distanceScale); michael@0: } michael@0: michael@0: switch (mCalibration.coverageCalibration) { michael@0: case Calibration::COVERAGE_CALIBRATION_NONE: michael@0: dump.append(INDENT4 "touch.coverage.calibration: none\n"); michael@0: break; michael@0: case Calibration::COVERAGE_CALIBRATION_BOX: michael@0: dump.append(INDENT4 "touch.coverage.calibration: box\n"); michael@0: break; michael@0: default: michael@0: ALOG_ASSERT(false); michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::reset(nsecs_t when) { michael@0: mCursorButtonAccumulator.reset(getDevice()); michael@0: mCursorScrollAccumulator.reset(getDevice()); michael@0: mTouchButtonAccumulator.reset(getDevice()); michael@0: michael@0: mPointerVelocityControl.reset(); michael@0: mWheelXVelocityControl.reset(); michael@0: mWheelYVelocityControl.reset(); michael@0: michael@0: mCurrentRawPointerData.clear(); michael@0: mLastRawPointerData.clear(); michael@0: mCurrentCookedPointerData.clear(); michael@0: mLastCookedPointerData.clear(); michael@0: mCurrentButtonState = 0; michael@0: mLastButtonState = 0; michael@0: mCurrentRawVScroll = 0; michael@0: mCurrentRawHScroll = 0; michael@0: mCurrentFingerIdBits.clear(); michael@0: mLastFingerIdBits.clear(); michael@0: mCurrentStylusIdBits.clear(); michael@0: mLastStylusIdBits.clear(); michael@0: mCurrentMouseIdBits.clear(); michael@0: mLastMouseIdBits.clear(); michael@0: mPointerUsage = POINTER_USAGE_NONE; michael@0: mSentHoverEnter = false; michael@0: mDownTime = 0; michael@0: michael@0: mCurrentVirtualKey.down = false; michael@0: michael@0: mPointerGesture.reset(); michael@0: mPointerSimple.reset(); michael@0: michael@0: if (mPointerController != NULL) { michael@0: mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); michael@0: mPointerController->clearSpots(); michael@0: } michael@0: michael@0: InputMapper::reset(when); michael@0: } michael@0: michael@0: void TouchInputMapper::process(const RawEvent* rawEvent) { michael@0: mCursorButtonAccumulator.process(rawEvent); michael@0: mCursorScrollAccumulator.process(rawEvent); michael@0: mTouchButtonAccumulator.process(rawEvent); michael@0: michael@0: if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { michael@0: sync(rawEvent->when); michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::sync(nsecs_t when) { michael@0: // Sync button state. michael@0: mCurrentButtonState = mTouchButtonAccumulator.getButtonState() michael@0: | mCursorButtonAccumulator.getButtonState(); michael@0: michael@0: // Sync scroll state. michael@0: mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel(); michael@0: mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel(); michael@0: mCursorScrollAccumulator.finishSync(); michael@0: michael@0: // Sync touch state. michael@0: bool havePointerIds = true; michael@0: mCurrentRawPointerData.clear(); michael@0: syncTouch(when, &havePointerIds); michael@0: michael@0: #if DEBUG_RAW_EVENTS michael@0: if (!havePointerIds) { michael@0: ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids", michael@0: mLastRawPointerData.pointerCount, michael@0: mCurrentRawPointerData.pointerCount); michael@0: } else { michael@0: ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, " michael@0: "hovering ids 0x%08x -> 0x%08x", michael@0: mLastRawPointerData.pointerCount, michael@0: mCurrentRawPointerData.pointerCount, michael@0: mLastRawPointerData.touchingIdBits.value, michael@0: mCurrentRawPointerData.touchingIdBits.value, michael@0: mLastRawPointerData.hoveringIdBits.value, michael@0: mCurrentRawPointerData.hoveringIdBits.value); michael@0: } michael@0: #endif michael@0: michael@0: // Reset state that we will compute below. michael@0: mCurrentFingerIdBits.clear(); michael@0: mCurrentStylusIdBits.clear(); michael@0: mCurrentMouseIdBits.clear(); michael@0: mCurrentCookedPointerData.clear(); michael@0: michael@0: if (mDeviceMode == DEVICE_MODE_DISABLED) { michael@0: // Drop all input if the device is disabled. michael@0: mCurrentRawPointerData.clear(); michael@0: mCurrentButtonState = 0; michael@0: } else { michael@0: // Preprocess pointer data. michael@0: if (!havePointerIds) { michael@0: assignPointerIds(); michael@0: } michael@0: michael@0: // Handle policy on initial down or hover events. michael@0: uint32_t policyFlags = 0; michael@0: bool initialDown = mLastRawPointerData.pointerCount == 0 michael@0: && mCurrentRawPointerData.pointerCount != 0; michael@0: bool buttonsPressed = mCurrentButtonState & ~mLastButtonState; michael@0: if (initialDown || buttonsPressed) { michael@0: // If this is a touch screen, hide the pointer on an initial down. michael@0: if (mDeviceMode == DEVICE_MODE_DIRECT) { michael@0: getContext()->fadePointer(); michael@0: } michael@0: michael@0: // Initial downs on external touch devices should wake the device. michael@0: // We don't do this for internal touch screens to prevent them from waking michael@0: // up in your pocket. michael@0: // TODO: Use the input device configuration to control this behavior more finely. michael@0: if (getDevice()->isExternal()) { michael@0: policyFlags |= POLICY_FLAG_WAKE_DROPPED; michael@0: } michael@0: } michael@0: michael@0: // Synthesize key down from raw buttons if needed. michael@0: synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, michael@0: policyFlags, mLastButtonState, mCurrentButtonState); michael@0: michael@0: // Consume raw off-screen touches before cooking pointer data. michael@0: // If touches are consumed, subsequent code will not receive any pointer data. michael@0: if (consumeRawTouches(when, policyFlags)) { michael@0: mCurrentRawPointerData.clear(); michael@0: } michael@0: michael@0: // Cook pointer data. This call populates the mCurrentCookedPointerData structure michael@0: // with cooked pointer data that has the same ids and indices as the raw data. michael@0: // The following code can use either the raw or cooked data, as needed. michael@0: cookPointerData(); michael@0: michael@0: // Dispatch the touches either directly or by translation through a pointer on screen. michael@0: if (mDeviceMode == DEVICE_MODE_POINTER) { michael@0: for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); michael@0: if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS michael@0: || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { michael@0: mCurrentStylusIdBits.markBit(id); michael@0: } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER michael@0: || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { michael@0: mCurrentFingerIdBits.markBit(id); michael@0: } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) { michael@0: mCurrentMouseIdBits.markBit(id); michael@0: } michael@0: } michael@0: for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); michael@0: if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS michael@0: || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { michael@0: mCurrentStylusIdBits.markBit(id); michael@0: } michael@0: } michael@0: michael@0: // Stylus takes precedence over all tools, then mouse, then finger. michael@0: PointerUsage pointerUsage = mPointerUsage; michael@0: if (!mCurrentStylusIdBits.isEmpty()) { michael@0: mCurrentMouseIdBits.clear(); michael@0: mCurrentFingerIdBits.clear(); michael@0: pointerUsage = POINTER_USAGE_STYLUS; michael@0: } else if (!mCurrentMouseIdBits.isEmpty()) { michael@0: mCurrentFingerIdBits.clear(); michael@0: pointerUsage = POINTER_USAGE_MOUSE; michael@0: } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) { michael@0: pointerUsage = POINTER_USAGE_GESTURES; michael@0: } michael@0: michael@0: dispatchPointerUsage(when, policyFlags, pointerUsage); michael@0: } else { michael@0: if (mDeviceMode == DEVICE_MODE_DIRECT michael@0: && mConfig.showTouches && mPointerController != NULL) { michael@0: mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); michael@0: mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); michael@0: michael@0: mPointerController->setButtonState(mCurrentButtonState); michael@0: mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords, michael@0: mCurrentCookedPointerData.idToIndex, michael@0: mCurrentCookedPointerData.touchingIdBits); michael@0: } michael@0: michael@0: dispatchHoverExit(when, policyFlags); michael@0: dispatchTouches(when, policyFlags); michael@0: dispatchHoverEnterAndMove(when, policyFlags); michael@0: } michael@0: michael@0: // Synthesize key up from raw buttons if needed. michael@0: synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, michael@0: policyFlags, mLastButtonState, mCurrentButtonState); michael@0: } michael@0: michael@0: // Copy current touch to last touch in preparation for the next cycle. michael@0: mLastRawPointerData.copyFrom(mCurrentRawPointerData); michael@0: mLastCookedPointerData.copyFrom(mCurrentCookedPointerData); michael@0: mLastButtonState = mCurrentButtonState; michael@0: mLastFingerIdBits = mCurrentFingerIdBits; michael@0: mLastStylusIdBits = mCurrentStylusIdBits; michael@0: mLastMouseIdBits = mCurrentMouseIdBits; michael@0: michael@0: // Clear some transient state. michael@0: mCurrentRawVScroll = 0; michael@0: mCurrentRawHScroll = 0; michael@0: } michael@0: michael@0: void TouchInputMapper::timeoutExpired(nsecs_t when) { michael@0: if (mDeviceMode == DEVICE_MODE_POINTER) { michael@0: if (mPointerUsage == POINTER_USAGE_GESTURES) { michael@0: dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) { michael@0: // Check for release of a virtual key. michael@0: if (mCurrentVirtualKey.down) { michael@0: if (mCurrentRawPointerData.touchingIdBits.isEmpty()) { michael@0: // Pointer went up while virtual key was down. michael@0: mCurrentVirtualKey.down = false; michael@0: if (!mCurrentVirtualKey.ignored) { michael@0: #if DEBUG_VIRTUAL_KEYS michael@0: ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", michael@0: mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); michael@0: #endif michael@0: dispatchVirtualKey(when, policyFlags, michael@0: AKEY_EVENT_ACTION_UP, michael@0: AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: if (mCurrentRawPointerData.touchingIdBits.count() == 1) { michael@0: uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); michael@0: const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); michael@0: const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); michael@0: if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) { michael@0: // Pointer is still within the space of the virtual key. michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: // Pointer left virtual key area or another pointer also went down. michael@0: // Send key cancellation but do not consume the touch yet. michael@0: // This is useful when the user swipes through from the virtual key area michael@0: // into the main display surface. michael@0: mCurrentVirtualKey.down = false; michael@0: if (!mCurrentVirtualKey.ignored) { michael@0: #if DEBUG_VIRTUAL_KEYS michael@0: ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", michael@0: mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); michael@0: #endif michael@0: dispatchVirtualKey(when, policyFlags, michael@0: AKEY_EVENT_ACTION_UP, michael@0: AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY michael@0: | AKEY_EVENT_FLAG_CANCELED); michael@0: } michael@0: } michael@0: michael@0: if (mLastRawPointerData.touchingIdBits.isEmpty() michael@0: && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { michael@0: // Pointer just went down. Check for virtual key press or off-screen touches. michael@0: uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); michael@0: const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); michael@0: if (!isPointInsideSurface(pointer.x, pointer.y)) { michael@0: // If exactly one pointer went down, check for virtual key hit. michael@0: // Otherwise we will drop the entire stroke. michael@0: if (mCurrentRawPointerData.touchingIdBits.count() == 1) { michael@0: const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); michael@0: if (virtualKey) { michael@0: mCurrentVirtualKey.down = true; michael@0: mCurrentVirtualKey.downTime = when; michael@0: mCurrentVirtualKey.keyCode = virtualKey->keyCode; michael@0: mCurrentVirtualKey.scanCode = virtualKey->scanCode; michael@0: mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey( michael@0: when, getDevice(), virtualKey->keyCode, virtualKey->scanCode); michael@0: michael@0: if (!mCurrentVirtualKey.ignored) { michael@0: #if DEBUG_VIRTUAL_KEYS michael@0: ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", michael@0: mCurrentVirtualKey.keyCode, michael@0: mCurrentVirtualKey.scanCode); michael@0: #endif michael@0: dispatchVirtualKey(when, policyFlags, michael@0: AKEY_EVENT_ACTION_DOWN, michael@0: AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: // Disable all virtual key touches that happen within a short time interval of the michael@0: // most recent touch within the screen area. The idea is to filter out stray michael@0: // virtual key presses when interacting with the touch screen. michael@0: // michael@0: // Problems we're trying to solve: michael@0: // michael@0: // 1. While scrolling a list or dragging the window shade, the user swipes down into a michael@0: // virtual key area that is implemented by a separate touch panel and accidentally michael@0: // triggers a virtual key. michael@0: // michael@0: // 2. While typing in the on screen keyboard, the user taps slightly outside the screen michael@0: // area and accidentally triggers a virtual key. This often happens when virtual keys michael@0: // are layed out below the screen near to where the on screen keyboard's space bar michael@0: // is displayed. michael@0: if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { michael@0: mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, michael@0: int32_t keyEventAction, int32_t keyEventFlags) { michael@0: int32_t keyCode = mCurrentVirtualKey.keyCode; michael@0: int32_t scanCode = mCurrentVirtualKey.scanCode; michael@0: nsecs_t downTime = mCurrentVirtualKey.downTime; michael@0: int32_t metaState = mContext->getGlobalMetaState(); michael@0: policyFlags |= POLICY_FLAG_VIRTUAL; michael@0: michael@0: NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, michael@0: keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); michael@0: getListener()->notifyKey(&args); michael@0: } michael@0: michael@0: void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { michael@0: BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits; michael@0: BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits; michael@0: int32_t metaState = getContext()->getGlobalMetaState(); michael@0: int32_t buttonState = mCurrentButtonState; michael@0: michael@0: if (currentIdBits == lastIdBits) { michael@0: if (!currentIdBits.isEmpty()) { michael@0: // No pointer id changes so this is a move event. michael@0: // The listener takes care of batching moves so we don't have to deal with that here. michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, michael@0: AMOTION_EVENT_EDGE_FLAG_NONE, michael@0: mCurrentCookedPointerData.pointerProperties, michael@0: mCurrentCookedPointerData.pointerCoords, michael@0: mCurrentCookedPointerData.idToIndex, michael@0: currentIdBits, -1, michael@0: mOrientedXPrecision, mOrientedYPrecision, mDownTime); michael@0: } michael@0: } else { michael@0: // There may be pointers going up and pointers going down and pointers moving michael@0: // all at the same time. michael@0: BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); michael@0: BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); michael@0: BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); michael@0: BitSet32 dispatchedIdBits(lastIdBits.value); michael@0: michael@0: // Update last coordinates of pointers that have moved so that we observe the new michael@0: // pointer positions at the same time as other pointers that have just gone up. michael@0: bool moveNeeded = updateMovedPointers( michael@0: mCurrentCookedPointerData.pointerProperties, michael@0: mCurrentCookedPointerData.pointerCoords, michael@0: mCurrentCookedPointerData.idToIndex, michael@0: mLastCookedPointerData.pointerProperties, michael@0: mLastCookedPointerData.pointerCoords, michael@0: mLastCookedPointerData.idToIndex, michael@0: moveIdBits); michael@0: if (buttonState != mLastButtonState) { michael@0: moveNeeded = true; michael@0: } michael@0: michael@0: // Dispatch pointer up events. michael@0: while (!upIdBits.isEmpty()) { michael@0: uint32_t upId = upIdBits.clearFirstMarkedBit(); michael@0: michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0, michael@0: mLastCookedPointerData.pointerProperties, michael@0: mLastCookedPointerData.pointerCoords, michael@0: mLastCookedPointerData.idToIndex, michael@0: dispatchedIdBits, upId, michael@0: mOrientedXPrecision, mOrientedYPrecision, mDownTime); michael@0: dispatchedIdBits.clearBit(upId); michael@0: } michael@0: michael@0: // Dispatch move events if any of the remaining pointers moved from their old locations. michael@0: // Although applications receive new locations as part of individual pointer up michael@0: // events, they do not generally handle them except when presented in a move event. michael@0: if (moveNeeded) { michael@0: ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0, michael@0: mCurrentCookedPointerData.pointerProperties, michael@0: mCurrentCookedPointerData.pointerCoords, michael@0: mCurrentCookedPointerData.idToIndex, michael@0: dispatchedIdBits, -1, michael@0: mOrientedXPrecision, mOrientedYPrecision, mDownTime); michael@0: } michael@0: michael@0: // Dispatch pointer down events using the new pointer locations. michael@0: while (!downIdBits.isEmpty()) { michael@0: uint32_t downId = downIdBits.clearFirstMarkedBit(); michael@0: dispatchedIdBits.markBit(downId); michael@0: michael@0: if (dispatchedIdBits.count() == 1) { michael@0: // First pointer is going down. Set down time. michael@0: mDownTime = when; michael@0: } michael@0: michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, michael@0: mCurrentCookedPointerData.pointerProperties, michael@0: mCurrentCookedPointerData.pointerCoords, michael@0: mCurrentCookedPointerData.idToIndex, michael@0: dispatchedIdBits, downId, michael@0: mOrientedXPrecision, mOrientedYPrecision, mDownTime); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) { michael@0: if (mSentHoverEnter && michael@0: (mCurrentCookedPointerData.hoveringIdBits.isEmpty() michael@0: || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) { michael@0: int32_t metaState = getContext()->getGlobalMetaState(); michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, michael@0: mLastCookedPointerData.pointerProperties, michael@0: mLastCookedPointerData.pointerCoords, michael@0: mLastCookedPointerData.idToIndex, michael@0: mLastCookedPointerData.hoveringIdBits, -1, michael@0: mOrientedXPrecision, mOrientedYPrecision, mDownTime); michael@0: mSentHoverEnter = false; michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) { michael@0: if (mCurrentCookedPointerData.touchingIdBits.isEmpty() michael@0: && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) { michael@0: int32_t metaState = getContext()->getGlobalMetaState(); michael@0: if (!mSentHoverEnter) { michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, michael@0: mCurrentCookedPointerData.pointerProperties, michael@0: mCurrentCookedPointerData.pointerCoords, michael@0: mCurrentCookedPointerData.idToIndex, michael@0: mCurrentCookedPointerData.hoveringIdBits, -1, michael@0: mOrientedXPrecision, mOrientedYPrecision, mDownTime); michael@0: mSentHoverEnter = true; michael@0: } michael@0: michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, michael@0: mCurrentCookedPointerData.pointerProperties, michael@0: mCurrentCookedPointerData.pointerCoords, michael@0: mCurrentCookedPointerData.idToIndex, michael@0: mCurrentCookedPointerData.hoveringIdBits, -1, michael@0: mOrientedXPrecision, mOrientedYPrecision, mDownTime); michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::cookPointerData() { michael@0: uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; michael@0: michael@0: mCurrentCookedPointerData.clear(); michael@0: mCurrentCookedPointerData.pointerCount = currentPointerCount; michael@0: mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits; michael@0: mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits; michael@0: michael@0: // Walk through the the active pointers and map device coordinates onto michael@0: // surface coordinates and adjust for display orientation. michael@0: for (uint32_t i = 0; i < currentPointerCount; i++) { michael@0: const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i]; michael@0: michael@0: // Size michael@0: float touchMajor, touchMinor, toolMajor, toolMinor, size; michael@0: switch (mCalibration.sizeCalibration) { michael@0: case Calibration::SIZE_CALIBRATION_GEOMETRIC: michael@0: case Calibration::SIZE_CALIBRATION_DIAMETER: michael@0: case Calibration::SIZE_CALIBRATION_BOX: michael@0: case Calibration::SIZE_CALIBRATION_AREA: michael@0: if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) { michael@0: touchMajor = in.touchMajor; michael@0: touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; michael@0: toolMajor = in.toolMajor; michael@0: toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; michael@0: size = mRawPointerAxes.touchMinor.valid michael@0: ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; michael@0: } else if (mRawPointerAxes.touchMajor.valid) { michael@0: toolMajor = touchMajor = in.touchMajor; michael@0: toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid michael@0: ? in.touchMinor : in.touchMajor; michael@0: size = mRawPointerAxes.touchMinor.valid michael@0: ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; michael@0: } else if (mRawPointerAxes.toolMajor.valid) { michael@0: touchMajor = toolMajor = in.toolMajor; michael@0: touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid michael@0: ? in.toolMinor : in.toolMajor; michael@0: size = mRawPointerAxes.toolMinor.valid michael@0: ? avg(in.toolMajor, in.toolMinor) : in.toolMajor; michael@0: } else { michael@0: ALOG_ASSERT(false, "No touch or tool axes. " michael@0: "Size calibration should have been resolved to NONE."); michael@0: touchMajor = 0; michael@0: touchMinor = 0; michael@0: toolMajor = 0; michael@0: toolMinor = 0; michael@0: size = 0; michael@0: } michael@0: michael@0: if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) { michael@0: uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count(); michael@0: if (touchingCount > 1) { michael@0: touchMajor /= touchingCount; michael@0: touchMinor /= touchingCount; michael@0: toolMajor /= touchingCount; michael@0: toolMinor /= touchingCount; michael@0: size /= touchingCount; michael@0: } michael@0: } michael@0: michael@0: if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) { michael@0: touchMajor *= mGeometricScale; michael@0: touchMinor *= mGeometricScale; michael@0: toolMajor *= mGeometricScale; michael@0: toolMinor *= mGeometricScale; michael@0: } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) { michael@0: touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0; michael@0: touchMinor = touchMajor; michael@0: toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0; michael@0: toolMinor = toolMajor; michael@0: } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) { michael@0: touchMinor = touchMajor; michael@0: toolMinor = toolMajor; michael@0: } michael@0: michael@0: mCalibration.applySizeScaleAndBias(&touchMajor); michael@0: mCalibration.applySizeScaleAndBias(&touchMinor); michael@0: mCalibration.applySizeScaleAndBias(&toolMajor); michael@0: mCalibration.applySizeScaleAndBias(&toolMinor); michael@0: size *= mSizeScale; michael@0: break; michael@0: default: michael@0: touchMajor = 0; michael@0: touchMinor = 0; michael@0: toolMajor = 0; michael@0: toolMinor = 0; michael@0: size = 0; michael@0: break; michael@0: } michael@0: michael@0: // Pressure michael@0: float pressure; michael@0: switch (mCalibration.pressureCalibration) { michael@0: case Calibration::PRESSURE_CALIBRATION_PHYSICAL: michael@0: case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: michael@0: pressure = in.pressure * mPressureScale; michael@0: break; michael@0: default: michael@0: pressure = in.isHovering ? 0 : 1; michael@0: break; michael@0: } michael@0: michael@0: // Tilt and Orientation michael@0: float tilt; michael@0: float orientation; michael@0: if (mHaveTilt) { michael@0: float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale; michael@0: float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale; michael@0: orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); michael@0: tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); michael@0: } else { michael@0: tilt = 0; michael@0: michael@0: switch (mCalibration.orientationCalibration) { michael@0: case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: michael@0: orientation = in.orientation * mOrientationScale; michael@0: break; michael@0: case Calibration::ORIENTATION_CALIBRATION_VECTOR: { michael@0: int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); michael@0: int32_t c2 = signExtendNybble(in.orientation & 0x0f); michael@0: if (c1 != 0 || c2 != 0) { michael@0: orientation = atan2f(c1, c2) * 0.5f; michael@0: float confidence = hypotf(c1, c2); michael@0: float scale = 1.0f + confidence / 16.0f; michael@0: touchMajor *= scale; michael@0: touchMinor /= scale; michael@0: toolMajor *= scale; michael@0: toolMinor /= scale; michael@0: } else { michael@0: orientation = 0; michael@0: } michael@0: break; michael@0: } michael@0: default: michael@0: orientation = 0; michael@0: } michael@0: } michael@0: michael@0: // Distance michael@0: float distance; michael@0: switch (mCalibration.distanceCalibration) { michael@0: case Calibration::DISTANCE_CALIBRATION_SCALED: michael@0: distance = in.distance * mDistanceScale; michael@0: break; michael@0: default: michael@0: distance = 0; michael@0: } michael@0: michael@0: // Coverage michael@0: int32_t rawLeft, rawTop, rawRight, rawBottom; michael@0: switch (mCalibration.coverageCalibration) { michael@0: case Calibration::COVERAGE_CALIBRATION_BOX: michael@0: rawLeft = (in.toolMinor & 0xffff0000) >> 16; michael@0: rawRight = in.toolMinor & 0x0000ffff; michael@0: rawBottom = in.toolMajor & 0x0000ffff; michael@0: rawTop = (in.toolMajor & 0xffff0000) >> 16; michael@0: break; michael@0: default: michael@0: rawLeft = rawTop = rawRight = rawBottom = 0; michael@0: break; michael@0: } michael@0: michael@0: // X, Y, and the bounding box for coverage information michael@0: // Adjust coords for surface orientation. michael@0: float x, y, left, top, right, bottom; michael@0: switch (mSurfaceOrientation) { michael@0: case DISPLAY_ORIENTATION_90: michael@0: x = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; michael@0: y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate; michael@0: left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; michael@0: right = float(rawBottom- mRawPointerAxes.y.minValue) * mYScale + mYTranslate; michael@0: bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate; michael@0: top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate; michael@0: orientation -= M_PI_2; michael@0: if (orientation < - M_PI_2) { michael@0: orientation += M_PI; michael@0: } michael@0: break; michael@0: case DISPLAY_ORIENTATION_180: michael@0: x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate; michael@0: y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate; michael@0: left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate; michael@0: right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate; michael@0: bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate; michael@0: top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate; michael@0: break; michael@0: case DISPLAY_ORIENTATION_270: michael@0: x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate; michael@0: y = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; michael@0: left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate; michael@0: right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate; michael@0: bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; michael@0: top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; michael@0: orientation += M_PI_2; michael@0: if (orientation > M_PI_2) { michael@0: orientation -= M_PI; michael@0: } michael@0: break; michael@0: default: michael@0: x = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; michael@0: y = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; michael@0: left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; michael@0: right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; michael@0: bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; michael@0: top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; michael@0: break; michael@0: } michael@0: michael@0: // Write output coords. michael@0: PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i]; michael@0: out.clear(); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_X, x); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); michael@0: if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) { michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom); michael@0: } else { michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); michael@0: out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); michael@0: } michael@0: michael@0: // Write output properties. michael@0: PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i]; michael@0: uint32_t id = in.id; michael@0: properties.clear(); michael@0: properties.id = id; michael@0: properties.toolType = in.toolType; michael@0: michael@0: // Write id index. michael@0: mCurrentCookedPointerData.idToIndex[id] = i; michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, michael@0: PointerUsage pointerUsage) { michael@0: if (pointerUsage != mPointerUsage) { michael@0: abortPointerUsage(when, policyFlags); michael@0: mPointerUsage = pointerUsage; michael@0: } michael@0: michael@0: switch (mPointerUsage) { michael@0: case POINTER_USAGE_GESTURES: michael@0: dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); michael@0: break; michael@0: case POINTER_USAGE_STYLUS: michael@0: dispatchPointerStylus(when, policyFlags); michael@0: break; michael@0: case POINTER_USAGE_MOUSE: michael@0: dispatchPointerMouse(when, policyFlags); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) { michael@0: switch (mPointerUsage) { michael@0: case POINTER_USAGE_GESTURES: michael@0: abortPointerGestures(when, policyFlags); michael@0: break; michael@0: case POINTER_USAGE_STYLUS: michael@0: abortPointerStylus(when, policyFlags); michael@0: break; michael@0: case POINTER_USAGE_MOUSE: michael@0: abortPointerMouse(when, policyFlags); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: mPointerUsage = POINTER_USAGE_NONE; michael@0: } michael@0: michael@0: void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, michael@0: bool isTimeout) { michael@0: // Update current gesture coordinates. michael@0: bool cancelPreviousGesture, finishPreviousGesture; michael@0: bool sendEvents = preparePointerGestures(when, michael@0: &cancelPreviousGesture, &finishPreviousGesture, isTimeout); michael@0: if (!sendEvents) { michael@0: return; michael@0: } michael@0: if (finishPreviousGesture) { michael@0: cancelPreviousGesture = false; michael@0: } michael@0: michael@0: // Update the pointer presentation and spots. michael@0: if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { michael@0: mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); michael@0: if (finishPreviousGesture || cancelPreviousGesture) { michael@0: mPointerController->clearSpots(); michael@0: } michael@0: mPointerController->setSpots(mPointerGesture.currentGestureCoords, michael@0: mPointerGesture.currentGestureIdToIndex, michael@0: mPointerGesture.currentGestureIdBits); michael@0: } else { michael@0: mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); michael@0: } michael@0: michael@0: // Show or hide the pointer if needed. michael@0: switch (mPointerGesture.currentGestureMode) { michael@0: case PointerGesture::NEUTRAL: michael@0: case PointerGesture::QUIET: michael@0: if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS michael@0: && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE michael@0: || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) { michael@0: // Remind the user of where the pointer is after finishing a gesture with spots. michael@0: mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); michael@0: } michael@0: break; michael@0: case PointerGesture::TAP: michael@0: case PointerGesture::TAP_DRAG: michael@0: case PointerGesture::BUTTON_CLICK_OR_DRAG: michael@0: case PointerGesture::HOVER: michael@0: case PointerGesture::PRESS: michael@0: // Unfade the pointer when the current gesture manipulates the michael@0: // area directly under the pointer. michael@0: mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); michael@0: break; michael@0: case PointerGesture::SWIPE: michael@0: case PointerGesture::FREEFORM: michael@0: // Fade the pointer when the current gesture manipulates a different michael@0: // area and there are spots to guide the user experience. michael@0: if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { michael@0: mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); michael@0: } else { michael@0: mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); michael@0: } michael@0: break; michael@0: } michael@0: michael@0: // Send events! michael@0: int32_t metaState = getContext()->getGlobalMetaState(); michael@0: int32_t buttonState = mCurrentButtonState; michael@0: michael@0: // Update last coordinates of pointers that have moved so that we observe the new michael@0: // pointer positions at the same time as other pointers that have just gone up. michael@0: bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP michael@0: || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG michael@0: || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG michael@0: || mPointerGesture.currentGestureMode == PointerGesture::PRESS michael@0: || mPointerGesture.currentGestureMode == PointerGesture::SWIPE michael@0: || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; michael@0: bool moveNeeded = false; michael@0: if (down && !cancelPreviousGesture && !finishPreviousGesture michael@0: && !mPointerGesture.lastGestureIdBits.isEmpty() michael@0: && !mPointerGesture.currentGestureIdBits.isEmpty()) { michael@0: BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value michael@0: & mPointerGesture.lastGestureIdBits.value); michael@0: moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, michael@0: mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, michael@0: mPointerGesture.lastGestureProperties, michael@0: mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, michael@0: movedGestureIdBits); michael@0: if (buttonState != mLastButtonState) { michael@0: moveNeeded = true; michael@0: } michael@0: } michael@0: michael@0: // Send motion events for all pointers that went up or were canceled. michael@0: BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); michael@0: if (!dispatchedGestureIdBits.isEmpty()) { michael@0: if (cancelPreviousGesture) { michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, michael@0: AMOTION_EVENT_EDGE_FLAG_NONE, michael@0: mPointerGesture.lastGestureProperties, michael@0: mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, michael@0: dispatchedGestureIdBits, -1, michael@0: 0, 0, mPointerGesture.downTime); michael@0: michael@0: dispatchedGestureIdBits.clear(); michael@0: } else { michael@0: BitSet32 upGestureIdBits; michael@0: if (finishPreviousGesture) { michael@0: upGestureIdBits = dispatchedGestureIdBits; michael@0: } else { michael@0: upGestureIdBits.value = dispatchedGestureIdBits.value michael@0: & ~mPointerGesture.currentGestureIdBits.value; michael@0: } michael@0: while (!upGestureIdBits.isEmpty()) { michael@0: uint32_t id = upGestureIdBits.clearFirstMarkedBit(); michael@0: michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_POINTER_UP, 0, michael@0: metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, michael@0: mPointerGesture.lastGestureProperties, michael@0: mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, michael@0: dispatchedGestureIdBits, id, michael@0: 0, 0, mPointerGesture.downTime); michael@0: michael@0: dispatchedGestureIdBits.clearBit(id); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Send motion events for all pointers that moved. michael@0: if (moveNeeded) { michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, michael@0: mPointerGesture.currentGestureProperties, michael@0: mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, michael@0: dispatchedGestureIdBits, -1, michael@0: 0, 0, mPointerGesture.downTime); michael@0: } michael@0: michael@0: // Send motion events for all pointers that went down. michael@0: if (down) { michael@0: BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value michael@0: & ~dispatchedGestureIdBits.value); michael@0: while (!downGestureIdBits.isEmpty()) { michael@0: uint32_t id = downGestureIdBits.clearFirstMarkedBit(); michael@0: dispatchedGestureIdBits.markBit(id); michael@0: michael@0: if (dispatchedGestureIdBits.count() == 1) { michael@0: mPointerGesture.downTime = when; michael@0: } michael@0: michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, michael@0: mPointerGesture.currentGestureProperties, michael@0: mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, michael@0: dispatchedGestureIdBits, id, michael@0: 0, 0, mPointerGesture.downTime); michael@0: } michael@0: } michael@0: michael@0: // Send motion events for hover. michael@0: if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_HOVER_MOVE, 0, michael@0: metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, michael@0: mPointerGesture.currentGestureProperties, michael@0: mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, michael@0: mPointerGesture.currentGestureIdBits, -1, michael@0: 0, 0, mPointerGesture.downTime); michael@0: } else if (dispatchedGestureIdBits.isEmpty() michael@0: && !mPointerGesture.lastGestureIdBits.isEmpty()) { michael@0: // Synthesize a hover move event after all pointers go up to indicate that michael@0: // the pointer is hovering again even if the user is not currently touching michael@0: // the touch pad. This ensures that a view will receive a fresh hover enter michael@0: // event after a tap. michael@0: float x, y; michael@0: mPointerController->getPosition(&x, &y); michael@0: michael@0: PointerProperties pointerProperties; michael@0: pointerProperties.clear(); michael@0: pointerProperties.id = 0; michael@0: pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; michael@0: michael@0: PointerCoords pointerCoords; michael@0: pointerCoords.clear(); michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); michael@0: michael@0: NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, michael@0: AMOTION_EVENT_ACTION_HOVER_MOVE, 0, michael@0: metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, michael@0: mViewport.displayId, 1, &pointerProperties, &pointerCoords, michael@0: 0, 0, mPointerGesture.downTime); michael@0: getListener()->notifyMotion(&args); michael@0: } michael@0: michael@0: // Update state. michael@0: mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; michael@0: if (!down) { michael@0: mPointerGesture.lastGestureIdBits.clear(); michael@0: } else { michael@0: mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; michael@0: for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; michael@0: mPointerGesture.lastGestureProperties[index].copyFrom( michael@0: mPointerGesture.currentGestureProperties[index]); michael@0: mPointerGesture.lastGestureCoords[index].copyFrom( michael@0: mPointerGesture.currentGestureCoords[index]); michael@0: mPointerGesture.lastGestureIdToIndex[id] = index; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) { michael@0: // Cancel previously dispatches pointers. michael@0: if (!mPointerGesture.lastGestureIdBits.isEmpty()) { michael@0: int32_t metaState = getContext()->getGlobalMetaState(); michael@0: int32_t buttonState = mCurrentButtonState; michael@0: dispatchMotion(when, policyFlags, mSource, michael@0: AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, michael@0: AMOTION_EVENT_EDGE_FLAG_NONE, michael@0: mPointerGesture.lastGestureProperties, michael@0: mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, michael@0: mPointerGesture.lastGestureIdBits, -1, michael@0: 0, 0, mPointerGesture.downTime); michael@0: } michael@0: michael@0: // Reset the current pointer gesture. michael@0: mPointerGesture.reset(); michael@0: mPointerVelocityControl.reset(); michael@0: michael@0: // Remove any current spots. michael@0: if (mPointerController != NULL) { michael@0: mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); michael@0: mPointerController->clearSpots(); michael@0: } michael@0: } michael@0: michael@0: bool TouchInputMapper::preparePointerGestures(nsecs_t when, michael@0: bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) { michael@0: *outCancelPreviousGesture = false; michael@0: *outFinishPreviousGesture = false; michael@0: michael@0: // Handle TAP timeout. michael@0: if (isTimeout) { michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: Processing timeout"); michael@0: #endif michael@0: michael@0: if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { michael@0: if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { michael@0: // The tap/drag timeout has not yet expired. michael@0: getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime michael@0: + mConfig.pointerGestureTapDragInterval); michael@0: } else { michael@0: // The tap is finished. michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: TAP finished"); michael@0: #endif michael@0: *outFinishPreviousGesture = true; michael@0: michael@0: mPointerGesture.activeGestureId = -1; michael@0: mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; michael@0: mPointerGesture.currentGestureIdBits.clear(); michael@0: michael@0: mPointerVelocityControl.reset(); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: // We did not handle this timeout. michael@0: return false; michael@0: } michael@0: michael@0: const uint32_t currentFingerCount = mCurrentFingerIdBits.count(); michael@0: const uint32_t lastFingerCount = mLastFingerIdBits.count(); michael@0: michael@0: // Update the velocity tracker. michael@0: { michael@0: VelocityTracker::Position positions[MAX_POINTERS]; michael@0: uint32_t count = 0; michael@0: for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); michael@0: positions[count].x = pointer.x * mPointerXMovementScale; michael@0: positions[count].y = pointer.y * mPointerYMovementScale; michael@0: } michael@0: mPointerGesture.velocityTracker.addMovement(when, michael@0: mCurrentFingerIdBits, positions); michael@0: } michael@0: michael@0: // Pick a new active touch id if needed. michael@0: // Choose an arbitrary pointer that just went down, if there is one. michael@0: // Otherwise choose an arbitrary remaining pointer. michael@0: // This guarantees we always have an active touch id when there is at least one pointer. michael@0: // We keep the same active touch id for as long as possible. michael@0: bool activeTouchChanged = false; michael@0: int32_t lastActiveTouchId = mPointerGesture.activeTouchId; michael@0: int32_t activeTouchId = lastActiveTouchId; michael@0: if (activeTouchId < 0) { michael@0: if (!mCurrentFingerIdBits.isEmpty()) { michael@0: activeTouchChanged = true; michael@0: activeTouchId = mPointerGesture.activeTouchId = michael@0: mCurrentFingerIdBits.firstMarkedBit(); michael@0: mPointerGesture.firstTouchTime = when; michael@0: } michael@0: } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) { michael@0: activeTouchChanged = true; michael@0: if (!mCurrentFingerIdBits.isEmpty()) { michael@0: activeTouchId = mPointerGesture.activeTouchId = michael@0: mCurrentFingerIdBits.firstMarkedBit(); michael@0: } else { michael@0: activeTouchId = mPointerGesture.activeTouchId = -1; michael@0: } michael@0: } michael@0: michael@0: // Determine whether we are in quiet time. michael@0: bool isQuietTime = false; michael@0: if (activeTouchId < 0) { michael@0: mPointerGesture.resetQuietTime(); michael@0: } else { michael@0: isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval; michael@0: if (!isQuietTime) { michael@0: if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS michael@0: || mPointerGesture.lastGestureMode == PointerGesture::SWIPE michael@0: || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) michael@0: && currentFingerCount < 2) { michael@0: // Enter quiet time when exiting swipe or freeform state. michael@0: // This is to prevent accidentally entering the hover state and flinging the michael@0: // pointer when finishing a swipe and there is still one pointer left onscreen. michael@0: isQuietTime = true; michael@0: } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG michael@0: && currentFingerCount >= 2 michael@0: && !isPointerDown(mCurrentButtonState)) { michael@0: // Enter quiet time when releasing the button and there are still two or more michael@0: // fingers down. This may indicate that one finger was used to press the button michael@0: // but it has not gone up yet. michael@0: isQuietTime = true; michael@0: } michael@0: if (isQuietTime) { michael@0: mPointerGesture.quietTime = when; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Switch states based on button and pointer state. michael@0: if (isQuietTime) { michael@0: // Case 1: Quiet time. (QUIET) michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime michael@0: + mConfig.pointerGestureQuietInterval - when) * 0.000001f); michael@0: #endif michael@0: if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) { michael@0: *outFinishPreviousGesture = true; michael@0: } michael@0: michael@0: mPointerGesture.activeGestureId = -1; michael@0: mPointerGesture.currentGestureMode = PointerGesture::QUIET; michael@0: mPointerGesture.currentGestureIdBits.clear(); michael@0: michael@0: mPointerVelocityControl.reset(); michael@0: } else if (isPointerDown(mCurrentButtonState)) { michael@0: // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) michael@0: // The pointer follows the active touch point. michael@0: // Emit DOWN, MOVE, UP events at the pointer location. michael@0: // michael@0: // Only the active touch matters; other fingers are ignored. This policy helps michael@0: // to handle the case where the user places a second finger on the touch pad michael@0: // to apply the necessary force to depress an integrated button below the surface. michael@0: // We don't want the second finger to be delivered to applications. michael@0: // michael@0: // For this to work well, we need to make sure to track the pointer that is really michael@0: // active. If the user first puts one finger down to click then adds another michael@0: // finger to drag then the active pointer should switch to the finger that is michael@0: // being dragged. michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " michael@0: "currentFingerCount=%d", activeTouchId, currentFingerCount); michael@0: #endif michael@0: // Reset state when just starting. michael@0: if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { michael@0: *outFinishPreviousGesture = true; michael@0: mPointerGesture.activeGestureId = 0; michael@0: } michael@0: michael@0: // Switch pointers if needed. michael@0: // Find the fastest pointer and follow it. michael@0: if (activeTouchId >= 0 && currentFingerCount > 1) { michael@0: int32_t bestId = -1; michael@0: float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; michael@0: for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: float vx, vy; michael@0: if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { michael@0: float speed = hypotf(vx, vy); michael@0: if (speed > bestSpeed) { michael@0: bestId = id; michael@0: bestSpeed = speed; michael@0: } michael@0: } michael@0: } michael@0: if (bestId >= 0 && bestId != activeTouchId) { michael@0: mPointerGesture.activeTouchId = activeTouchId = bestId; michael@0: activeTouchChanged = true; michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " michael@0: "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) { michael@0: const RawPointerData::Pointer& currentPointer = michael@0: mCurrentRawPointerData.pointerForId(activeTouchId); michael@0: const RawPointerData::Pointer& lastPointer = michael@0: mLastRawPointerData.pointerForId(activeTouchId); michael@0: float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; michael@0: float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; michael@0: michael@0: rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); michael@0: mPointerVelocityControl.move(when, &deltaX, &deltaY); michael@0: michael@0: // Move the pointer using a relative motion. michael@0: // When using spots, the click will occur at the position of the anchor michael@0: // spot and all other spots will move there. michael@0: mPointerController->move(deltaX, deltaY); michael@0: } else { michael@0: mPointerVelocityControl.reset(); michael@0: } michael@0: michael@0: float x, y; michael@0: mPointerController->getPosition(&x, &y); michael@0: michael@0: mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; michael@0: mPointerGesture.currentGestureIdBits.clear(); michael@0: mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); michael@0: mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; michael@0: mPointerGesture.currentGestureProperties[0].clear(); michael@0: mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; michael@0: mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; michael@0: mPointerGesture.currentGestureCoords[0].clear(); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); michael@0: } else if (currentFingerCount == 0) { michael@0: // Case 3. No fingers down and button is not pressed. (NEUTRAL) michael@0: if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) { michael@0: *outFinishPreviousGesture = true; michael@0: } michael@0: michael@0: // Watch for taps coming out of HOVER or TAP_DRAG mode. michael@0: // Checking for taps after TAP_DRAG allows us to detect double-taps. michael@0: bool tapped = false; michael@0: if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER michael@0: || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) michael@0: && lastFingerCount == 1) { michael@0: if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { michael@0: float x, y; michael@0: mPointerController->getPosition(&x, &y); michael@0: if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop michael@0: && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: TAP"); michael@0: #endif michael@0: michael@0: mPointerGesture.tapUpTime = when; michael@0: getContext()->requestTimeoutAtTime(when michael@0: + mConfig.pointerGestureTapDragInterval); michael@0: michael@0: mPointerGesture.activeGestureId = 0; michael@0: mPointerGesture.currentGestureMode = PointerGesture::TAP; michael@0: mPointerGesture.currentGestureIdBits.clear(); michael@0: mPointerGesture.currentGestureIdBits.markBit( michael@0: mPointerGesture.activeGestureId); michael@0: mPointerGesture.currentGestureIdToIndex[ michael@0: mPointerGesture.activeGestureId] = 0; michael@0: mPointerGesture.currentGestureProperties[0].clear(); michael@0: mPointerGesture.currentGestureProperties[0].id = michael@0: mPointerGesture.activeGestureId; michael@0: mPointerGesture.currentGestureProperties[0].toolType = michael@0: AMOTION_EVENT_TOOL_TYPE_FINGER; michael@0: mPointerGesture.currentGestureCoords[0].clear(); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue( michael@0: AMOTION_EVENT_AXIS_X, mPointerGesture.tapX); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue( michael@0: AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue( michael@0: AMOTION_EVENT_AXIS_PRESSURE, 1.0f); michael@0: michael@0: tapped = true; michael@0: } else { michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", michael@0: x - mPointerGesture.tapX, michael@0: y - mPointerGesture.tapY); michael@0: #endif michael@0: } michael@0: } else { michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: Not a TAP, %0.3fms since down", michael@0: (when - mPointerGesture.tapDownTime) * 0.000001f); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: mPointerVelocityControl.reset(); michael@0: michael@0: if (!tapped) { michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: NEUTRAL"); michael@0: #endif michael@0: mPointerGesture.activeGestureId = -1; michael@0: mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; michael@0: mPointerGesture.currentGestureIdBits.clear(); michael@0: } michael@0: } else if (currentFingerCount == 1) { michael@0: // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG) michael@0: // The pointer follows the active touch point. michael@0: // When in HOVER, emit HOVER_MOVE events at the pointer location. michael@0: // When in TAP_DRAG, emit MOVE events at the pointer location. michael@0: ALOG_ASSERT(activeTouchId >= 0); michael@0: michael@0: mPointerGesture.currentGestureMode = PointerGesture::HOVER; michael@0: if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { michael@0: if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { michael@0: float x, y; michael@0: mPointerController->getPosition(&x, &y); michael@0: if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop michael@0: && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { michael@0: mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; michael@0: } else { michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", michael@0: x - mPointerGesture.tapX, michael@0: y - mPointerGesture.tapY); michael@0: #endif michael@0: } michael@0: } else { michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", michael@0: (when - mPointerGesture.tapUpTime) * 0.000001f); michael@0: #endif michael@0: } michael@0: } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { michael@0: mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; michael@0: } michael@0: michael@0: if (mLastFingerIdBits.hasBit(activeTouchId)) { michael@0: const RawPointerData::Pointer& currentPointer = michael@0: mCurrentRawPointerData.pointerForId(activeTouchId); michael@0: const RawPointerData::Pointer& lastPointer = michael@0: mLastRawPointerData.pointerForId(activeTouchId); michael@0: float deltaX = (currentPointer.x - lastPointer.x) michael@0: * mPointerXMovementScale; michael@0: float deltaY = (currentPointer.y - lastPointer.y) michael@0: * mPointerYMovementScale; michael@0: michael@0: rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); michael@0: mPointerVelocityControl.move(when, &deltaX, &deltaY); michael@0: michael@0: // Move the pointer using a relative motion. michael@0: // When using spots, the hover or drag will occur at the position of the anchor spot. michael@0: mPointerController->move(deltaX, deltaY); michael@0: } else { michael@0: mPointerVelocityControl.reset(); michael@0: } michael@0: michael@0: bool down; michael@0: if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: TAP_DRAG"); michael@0: #endif michael@0: down = true; michael@0: } else { michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: HOVER"); michael@0: #endif michael@0: if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) { michael@0: *outFinishPreviousGesture = true; michael@0: } michael@0: mPointerGesture.activeGestureId = 0; michael@0: down = false; michael@0: } michael@0: michael@0: float x, y; michael@0: mPointerController->getPosition(&x, &y); michael@0: michael@0: mPointerGesture.currentGestureIdBits.clear(); michael@0: mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); michael@0: mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; michael@0: mPointerGesture.currentGestureProperties[0].clear(); michael@0: mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; michael@0: mPointerGesture.currentGestureProperties[0].toolType = michael@0: AMOTION_EVENT_TOOL_TYPE_FINGER; michael@0: mPointerGesture.currentGestureCoords[0].clear(); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, michael@0: down ? 1.0f : 0.0f); michael@0: michael@0: if (lastFingerCount == 0 && currentFingerCount != 0) { michael@0: mPointerGesture.resetTap(); michael@0: mPointerGesture.tapDownTime = when; michael@0: mPointerGesture.tapX = x; michael@0: mPointerGesture.tapY = y; michael@0: } michael@0: } else { michael@0: // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) michael@0: // We need to provide feedback for each finger that goes down so we cannot wait michael@0: // for the fingers to move before deciding what to do. michael@0: // michael@0: // The ambiguous case is deciding what to do when there are two fingers down but they michael@0: // have not moved enough to determine whether they are part of a drag or part of a michael@0: // freeform gesture, or just a press or long-press at the pointer location. michael@0: // michael@0: // When there are two fingers we start with the PRESS hypothesis and we generate a michael@0: // down at the pointer location. michael@0: // michael@0: // When the two fingers move enough or when additional fingers are added, we make michael@0: // a decision to transition into SWIPE or FREEFORM mode accordingly. michael@0: ALOG_ASSERT(activeTouchId >= 0); michael@0: michael@0: bool settled = when >= mPointerGesture.firstTouchTime michael@0: + mConfig.pointerGestureMultitouchSettleInterval; michael@0: if (mPointerGesture.lastGestureMode != PointerGesture::PRESS michael@0: && mPointerGesture.lastGestureMode != PointerGesture::SWIPE michael@0: && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { michael@0: *outFinishPreviousGesture = true; michael@0: } else if (!settled && currentFingerCount > lastFingerCount) { michael@0: // Additional pointers have gone down but not yet settled. michael@0: // Reset the gesture. michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " michael@0: "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime michael@0: + mConfig.pointerGestureMultitouchSettleInterval - when) michael@0: * 0.000001f); michael@0: #endif michael@0: *outCancelPreviousGesture = true; michael@0: } else { michael@0: // Continue previous gesture. michael@0: mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; michael@0: } michael@0: michael@0: if (*outFinishPreviousGesture || *outCancelPreviousGesture) { michael@0: mPointerGesture.currentGestureMode = PointerGesture::PRESS; michael@0: mPointerGesture.activeGestureId = 0; michael@0: mPointerGesture.referenceIdBits.clear(); michael@0: mPointerVelocityControl.reset(); michael@0: michael@0: // Use the centroid and pointer location as the reference points for the gesture. michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: Using centroid as reference for MULTITOUCH, " michael@0: "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime michael@0: + mConfig.pointerGestureMultitouchSettleInterval - when) michael@0: * 0.000001f); michael@0: #endif michael@0: mCurrentRawPointerData.getCentroidOfTouchingPointers( michael@0: &mPointerGesture.referenceTouchX, michael@0: &mPointerGesture.referenceTouchY); michael@0: mPointerController->getPosition(&mPointerGesture.referenceGestureX, michael@0: &mPointerGesture.referenceGestureY); michael@0: } michael@0: michael@0: // Clear the reference deltas for fingers not yet included in the reference calculation. michael@0: for (BitSet32 idBits(mCurrentFingerIdBits.value michael@0: & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: mPointerGesture.referenceDeltas[id].dx = 0; michael@0: mPointerGesture.referenceDeltas[id].dy = 0; michael@0: } michael@0: mPointerGesture.referenceIdBits = mCurrentFingerIdBits; michael@0: michael@0: // Add delta for all fingers and calculate a common movement delta. michael@0: float commonDeltaX = 0, commonDeltaY = 0; michael@0: BitSet32 commonIdBits(mLastFingerIdBits.value michael@0: & mCurrentFingerIdBits.value); michael@0: for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { michael@0: bool first = (idBits == commonIdBits); michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id); michael@0: const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id); michael@0: PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; michael@0: delta.dx += cpd.x - lpd.x; michael@0: delta.dy += cpd.y - lpd.y; michael@0: michael@0: if (first) { michael@0: commonDeltaX = delta.dx; michael@0: commonDeltaY = delta.dy; michael@0: } else { michael@0: commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); michael@0: commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); michael@0: } michael@0: } michael@0: michael@0: // Consider transitions from PRESS to SWIPE or MULTITOUCH. michael@0: if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { michael@0: float dist[MAX_POINTER_ID + 1]; michael@0: int32_t distOverThreshold = 0; michael@0: for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; michael@0: dist[id] = hypotf(delta.dx * mPointerXZoomScale, michael@0: delta.dy * mPointerYZoomScale); michael@0: if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { michael@0: distOverThreshold += 1; michael@0: } michael@0: } michael@0: michael@0: // Only transition when at least two pointers have moved further than michael@0: // the minimum distance threshold. michael@0: if (distOverThreshold >= 2) { michael@0: if (currentFingerCount > 2) { michael@0: // There are more than two pointers, switch to FREEFORM. michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", michael@0: currentFingerCount); michael@0: #endif michael@0: *outCancelPreviousGesture = true; michael@0: mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; michael@0: } else { michael@0: // There are exactly two pointers. michael@0: BitSet32 idBits(mCurrentFingerIdBits); michael@0: uint32_t id1 = idBits.clearFirstMarkedBit(); michael@0: uint32_t id2 = idBits.firstMarkedBit(); michael@0: const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1); michael@0: const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2); michael@0: float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y); michael@0: if (mutualDistance > mPointerGestureMaxSwipeWidth) { michael@0: // There are two pointers but they are too far apart for a SWIPE, michael@0: // switch to FREEFORM. michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", michael@0: mutualDistance, mPointerGestureMaxSwipeWidth); michael@0: #endif michael@0: *outCancelPreviousGesture = true; michael@0: mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; michael@0: } else { michael@0: // There are two pointers. Wait for both pointers to start moving michael@0: // before deciding whether this is a SWIPE or FREEFORM gesture. michael@0: float dist1 = dist[id1]; michael@0: float dist2 = dist[id2]; michael@0: if (dist1 >= mConfig.pointerGestureMultitouchMinDistance michael@0: && dist2 >= mConfig.pointerGestureMultitouchMinDistance) { michael@0: // Calculate the dot product of the displacement vectors. michael@0: // When the vectors are oriented in approximately the same direction, michael@0: // the angle betweeen them is near zero and the cosine of the angle michael@0: // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2). michael@0: PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; michael@0: PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; michael@0: float dx1 = delta1.dx * mPointerXZoomScale; michael@0: float dy1 = delta1.dy * mPointerYZoomScale; michael@0: float dx2 = delta2.dx * mPointerXZoomScale; michael@0: float dy2 = delta2.dy * mPointerYZoomScale; michael@0: float dot = dx1 * dx2 + dy1 * dy2; michael@0: float cosine = dot / (dist1 * dist2); // denominator always > 0 michael@0: if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { michael@0: // Pointers are moving in the same direction. Switch to SWIPE. michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: PRESS transitioned to SWIPE, " michael@0: "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " michael@0: "cosine %0.3f >= %0.3f", michael@0: dist1, mConfig.pointerGestureMultitouchMinDistance, michael@0: dist2, mConfig.pointerGestureMultitouchMinDistance, michael@0: cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); michael@0: #endif michael@0: mPointerGesture.currentGestureMode = PointerGesture::SWIPE; michael@0: } else { michael@0: // Pointers are moving in different directions. Switch to FREEFORM. michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: PRESS transitioned to FREEFORM, " michael@0: "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " michael@0: "cosine %0.3f < %0.3f", michael@0: dist1, mConfig.pointerGestureMultitouchMinDistance, michael@0: dist2, mConfig.pointerGestureMultitouchMinDistance, michael@0: cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); michael@0: #endif michael@0: *outCancelPreviousGesture = true; michael@0: mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { michael@0: // Switch from SWIPE to FREEFORM if additional pointers go down. michael@0: // Cancel previous gesture. michael@0: if (currentFingerCount > 2) { michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", michael@0: currentFingerCount); michael@0: #endif michael@0: *outCancelPreviousGesture = true; michael@0: mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; michael@0: } michael@0: } michael@0: michael@0: // Move the reference points based on the overall group motion of the fingers michael@0: // except in PRESS mode while waiting for a transition to occur. michael@0: if (mPointerGesture.currentGestureMode != PointerGesture::PRESS michael@0: && (commonDeltaX || commonDeltaY)) { michael@0: for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; michael@0: delta.dx = 0; michael@0: delta.dy = 0; michael@0: } michael@0: michael@0: mPointerGesture.referenceTouchX += commonDeltaX; michael@0: mPointerGesture.referenceTouchY += commonDeltaY; michael@0: michael@0: commonDeltaX *= mPointerXMovementScale; michael@0: commonDeltaY *= mPointerYMovementScale; michael@0: michael@0: rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY); michael@0: mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); michael@0: michael@0: mPointerGesture.referenceGestureX += commonDeltaX; michael@0: mPointerGesture.referenceGestureY += commonDeltaY; michael@0: } michael@0: michael@0: // Report gestures. michael@0: if (mPointerGesture.currentGestureMode == PointerGesture::PRESS michael@0: || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { michael@0: // PRESS or SWIPE mode. michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d," michael@0: "activeGestureId=%d, currentTouchPointerCount=%d", michael@0: activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); michael@0: #endif michael@0: ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); michael@0: michael@0: mPointerGesture.currentGestureIdBits.clear(); michael@0: mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); michael@0: mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; michael@0: mPointerGesture.currentGestureProperties[0].clear(); michael@0: mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; michael@0: mPointerGesture.currentGestureProperties[0].toolType = michael@0: AMOTION_EVENT_TOOL_TYPE_FINGER; michael@0: mPointerGesture.currentGestureCoords[0].clear(); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, michael@0: mPointerGesture.referenceGestureX); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, michael@0: mPointerGesture.referenceGestureY); michael@0: mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); michael@0: } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { michael@0: // FREEFORM mode. michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: FREEFORM activeTouchId=%d," michael@0: "activeGestureId=%d, currentTouchPointerCount=%d", michael@0: activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); michael@0: #endif michael@0: ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); michael@0: michael@0: mPointerGesture.currentGestureIdBits.clear(); michael@0: michael@0: BitSet32 mappedTouchIdBits; michael@0: BitSet32 usedGestureIdBits; michael@0: if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { michael@0: // Initially, assign the active gesture id to the active touch point michael@0: // if there is one. No other touch id bits are mapped yet. michael@0: if (!*outCancelPreviousGesture) { michael@0: mappedTouchIdBits.markBit(activeTouchId); michael@0: usedGestureIdBits.markBit(mPointerGesture.activeGestureId); michael@0: mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = michael@0: mPointerGesture.activeGestureId; michael@0: } else { michael@0: mPointerGesture.activeGestureId = -1; michael@0: } michael@0: } else { michael@0: // Otherwise, assume we mapped all touches from the previous frame. michael@0: // Reuse all mappings that are still applicable. michael@0: mappedTouchIdBits.value = mLastFingerIdBits.value michael@0: & mCurrentFingerIdBits.value; michael@0: usedGestureIdBits = mPointerGesture.lastGestureIdBits; michael@0: michael@0: // Check whether we need to choose a new active gesture id because the michael@0: // current went went up. michael@0: for (BitSet32 upTouchIdBits(mLastFingerIdBits.value michael@0: & ~mCurrentFingerIdBits.value); michael@0: !upTouchIdBits.isEmpty(); ) { michael@0: uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit(); michael@0: uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; michael@0: if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { michael@0: mPointerGesture.activeGestureId = -1; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: FREEFORM follow up " michael@0: "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " michael@0: "activeGestureId=%d", michael@0: mappedTouchIdBits.value, usedGestureIdBits.value, michael@0: mPointerGesture.activeGestureId); michael@0: #endif michael@0: michael@0: BitSet32 idBits(mCurrentFingerIdBits); michael@0: for (uint32_t i = 0; i < currentFingerCount; i++) { michael@0: uint32_t touchId = idBits.clearFirstMarkedBit(); michael@0: uint32_t gestureId; michael@0: if (!mappedTouchIdBits.hasBit(touchId)) { michael@0: gestureId = usedGestureIdBits.markFirstUnmarkedBit(); michael@0: mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: FREEFORM " michael@0: "new mapping for touch id %d -> gesture id %d", michael@0: touchId, gestureId); michael@0: #endif michael@0: } else { michael@0: gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: FREEFORM " michael@0: "existing mapping for touch id %d -> gesture id %d", michael@0: touchId, gestureId); michael@0: #endif michael@0: } michael@0: mPointerGesture.currentGestureIdBits.markBit(gestureId); michael@0: mPointerGesture.currentGestureIdToIndex[gestureId] = i; michael@0: michael@0: const RawPointerData::Pointer& pointer = michael@0: mCurrentRawPointerData.pointerForId(touchId); michael@0: float deltaX = (pointer.x - mPointerGesture.referenceTouchX) michael@0: * mPointerXZoomScale; michael@0: float deltaY = (pointer.y - mPointerGesture.referenceTouchY) michael@0: * mPointerYZoomScale; michael@0: rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); michael@0: michael@0: mPointerGesture.currentGestureProperties[i].clear(); michael@0: mPointerGesture.currentGestureProperties[i].id = gestureId; michael@0: mPointerGesture.currentGestureProperties[i].toolType = michael@0: AMOTION_EVENT_TOOL_TYPE_FINGER; michael@0: mPointerGesture.currentGestureCoords[i].clear(); michael@0: mPointerGesture.currentGestureCoords[i].setAxisValue( michael@0: AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX); michael@0: mPointerGesture.currentGestureCoords[i].setAxisValue( michael@0: AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY); michael@0: mPointerGesture.currentGestureCoords[i].setAxisValue( michael@0: AMOTION_EVENT_AXIS_PRESSURE, 1.0f); michael@0: } michael@0: michael@0: if (mPointerGesture.activeGestureId < 0) { michael@0: mPointerGesture.activeGestureId = michael@0: mPointerGesture.currentGestureIdBits.firstMarkedBit(); michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: FREEFORM new " michael@0: "activeGestureId=%d", mPointerGesture.activeGestureId); michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: michael@0: mPointerController->setButtonState(mCurrentButtonState); michael@0: michael@0: #if DEBUG_GESTURES michael@0: ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " michael@0: "currentGestureMode=%d, currentGestureIdBits=0x%08x, " michael@0: "lastGestureMode=%d, lastGestureIdBits=0x%08x", michael@0: toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), michael@0: mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, michael@0: mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); michael@0: for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; michael@0: const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; michael@0: const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; michael@0: ALOGD(" currentGesture[%d]: index=%d, toolType=%d, " michael@0: "x=%0.3f, y=%0.3f, pressure=%0.3f", michael@0: id, index, properties.toolType, michael@0: coords.getAxisValue(AMOTION_EVENT_AXIS_X), michael@0: coords.getAxisValue(AMOTION_EVENT_AXIS_Y), michael@0: coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); michael@0: } michael@0: for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; michael@0: const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; michael@0: const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; michael@0: ALOGD(" lastGesture[%d]: index=%d, toolType=%d, " michael@0: "x=%0.3f, y=%0.3f, pressure=%0.3f", michael@0: id, index, properties.toolType, michael@0: coords.getAxisValue(AMOTION_EVENT_AXIS_X), michael@0: coords.getAxisValue(AMOTION_EVENT_AXIS_Y), michael@0: coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); michael@0: } michael@0: #endif michael@0: return true; michael@0: } michael@0: michael@0: void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) { michael@0: mPointerSimple.currentCoords.clear(); michael@0: mPointerSimple.currentProperties.clear(); michael@0: michael@0: bool down, hovering; michael@0: if (!mCurrentStylusIdBits.isEmpty()) { michael@0: uint32_t id = mCurrentStylusIdBits.firstMarkedBit(); michael@0: uint32_t index = mCurrentCookedPointerData.idToIndex[id]; michael@0: float x = mCurrentCookedPointerData.pointerCoords[index].getX(); michael@0: float y = mCurrentCookedPointerData.pointerCoords[index].getY(); michael@0: mPointerController->setPosition(x, y); michael@0: michael@0: hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id); michael@0: down = !hovering; michael@0: michael@0: mPointerController->getPosition(&x, &y); michael@0: mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]); michael@0: mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); michael@0: mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); michael@0: mPointerSimple.currentProperties.id = 0; michael@0: mPointerSimple.currentProperties.toolType = michael@0: mCurrentCookedPointerData.pointerProperties[index].toolType; michael@0: } else { michael@0: down = false; michael@0: hovering = false; michael@0: } michael@0: michael@0: dispatchPointerSimple(when, policyFlags, down, hovering); michael@0: } michael@0: michael@0: void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) { michael@0: abortPointerSimple(when, policyFlags); michael@0: } michael@0: michael@0: void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) { michael@0: mPointerSimple.currentCoords.clear(); michael@0: mPointerSimple.currentProperties.clear(); michael@0: michael@0: bool down, hovering; michael@0: if (!mCurrentMouseIdBits.isEmpty()) { michael@0: uint32_t id = mCurrentMouseIdBits.firstMarkedBit(); michael@0: uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id]; michael@0: if (mLastMouseIdBits.hasBit(id)) { michael@0: uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id]; michael@0: float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x michael@0: - mLastRawPointerData.pointers[lastIndex].x) michael@0: * mPointerXMovementScale; michael@0: float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y michael@0: - mLastRawPointerData.pointers[lastIndex].y) michael@0: * mPointerYMovementScale; michael@0: michael@0: rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); michael@0: mPointerVelocityControl.move(when, &deltaX, &deltaY); michael@0: michael@0: mPointerController->move(deltaX, deltaY); michael@0: } else { michael@0: mPointerVelocityControl.reset(); michael@0: } michael@0: michael@0: down = isPointerDown(mCurrentButtonState); michael@0: hovering = !down; michael@0: michael@0: float x, y; michael@0: mPointerController->getPosition(&x, &y); michael@0: mPointerSimple.currentCoords.copyFrom( michael@0: mCurrentCookedPointerData.pointerCoords[currentIndex]); michael@0: mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); michael@0: mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); michael@0: mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, michael@0: hovering ? 0.0f : 1.0f); michael@0: mPointerSimple.currentProperties.id = 0; michael@0: mPointerSimple.currentProperties.toolType = michael@0: mCurrentCookedPointerData.pointerProperties[currentIndex].toolType; michael@0: } else { michael@0: mPointerVelocityControl.reset(); michael@0: michael@0: down = false; michael@0: hovering = false; michael@0: } michael@0: michael@0: dispatchPointerSimple(when, policyFlags, down, hovering); michael@0: } michael@0: michael@0: void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) { michael@0: abortPointerSimple(when, policyFlags); michael@0: michael@0: mPointerVelocityControl.reset(); michael@0: } michael@0: michael@0: void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, michael@0: bool down, bool hovering) { michael@0: int32_t metaState = getContext()->getGlobalMetaState(); michael@0: michael@0: if (mPointerController != NULL) { michael@0: if (down || hovering) { michael@0: mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); michael@0: mPointerController->clearSpots(); michael@0: mPointerController->setButtonState(mCurrentButtonState); michael@0: mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); michael@0: } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) { michael@0: mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); michael@0: } michael@0: } michael@0: michael@0: if (mPointerSimple.down && !down) { michael@0: mPointerSimple.down = false; michael@0: michael@0: // Send up. michael@0: NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, michael@0: AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0, michael@0: mViewport.displayId, michael@0: 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, michael@0: mOrientedXPrecision, mOrientedYPrecision, michael@0: mPointerSimple.downTime); michael@0: getListener()->notifyMotion(&args); michael@0: } michael@0: michael@0: if (mPointerSimple.hovering && !hovering) { michael@0: mPointerSimple.hovering = false; michael@0: michael@0: // Send hover exit. michael@0: NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, michael@0: AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, michael@0: mViewport.displayId, michael@0: 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, michael@0: mOrientedXPrecision, mOrientedYPrecision, michael@0: mPointerSimple.downTime); michael@0: getListener()->notifyMotion(&args); michael@0: } michael@0: michael@0: if (down) { michael@0: if (!mPointerSimple.down) { michael@0: mPointerSimple.down = true; michael@0: mPointerSimple.downTime = when; michael@0: michael@0: // Send down. michael@0: NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, michael@0: AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0, michael@0: mViewport.displayId, michael@0: 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, michael@0: mOrientedXPrecision, mOrientedYPrecision, michael@0: mPointerSimple.downTime); michael@0: getListener()->notifyMotion(&args); michael@0: } michael@0: michael@0: // Send move. michael@0: NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, michael@0: AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0, michael@0: mViewport.displayId, michael@0: 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, michael@0: mOrientedXPrecision, mOrientedYPrecision, michael@0: mPointerSimple.downTime); michael@0: getListener()->notifyMotion(&args); michael@0: } michael@0: michael@0: if (hovering) { michael@0: if (!mPointerSimple.hovering) { michael@0: mPointerSimple.hovering = true; michael@0: michael@0: // Send hover enter. michael@0: NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, michael@0: AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, michael@0: mViewport.displayId, michael@0: 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, michael@0: mOrientedXPrecision, mOrientedYPrecision, michael@0: mPointerSimple.downTime); michael@0: getListener()->notifyMotion(&args); michael@0: } michael@0: michael@0: // Send hover move. michael@0: NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, michael@0: AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, michael@0: mViewport.displayId, michael@0: 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, michael@0: mOrientedXPrecision, mOrientedYPrecision, michael@0: mPointerSimple.downTime); michael@0: getListener()->notifyMotion(&args); michael@0: } michael@0: michael@0: if (mCurrentRawVScroll || mCurrentRawHScroll) { michael@0: float vscroll = mCurrentRawVScroll; michael@0: float hscroll = mCurrentRawHScroll; michael@0: mWheelYVelocityControl.move(when, NULL, &vscroll); michael@0: mWheelXVelocityControl.move(when, &hscroll, NULL); michael@0: michael@0: // Send scroll. michael@0: PointerCoords pointerCoords; michael@0: pointerCoords.copyFrom(mPointerSimple.currentCoords); michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); michael@0: pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); michael@0: michael@0: NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, michael@0: AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0, michael@0: mViewport.displayId, michael@0: 1, &mPointerSimple.currentProperties, &pointerCoords, michael@0: mOrientedXPrecision, mOrientedYPrecision, michael@0: mPointerSimple.downTime); michael@0: getListener()->notifyMotion(&args); michael@0: } michael@0: michael@0: // Save state. michael@0: if (down || hovering) { michael@0: mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords); michael@0: mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties); michael@0: } else { michael@0: mPointerSimple.reset(); michael@0: } michael@0: } michael@0: michael@0: void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) { michael@0: mPointerSimple.currentCoords.clear(); michael@0: mPointerSimple.currentProperties.clear(); michael@0: michael@0: dispatchPointerSimple(when, policyFlags, false, false); michael@0: } michael@0: michael@0: void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, michael@0: int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, michael@0: const PointerProperties* properties, const PointerCoords* coords, michael@0: const uint32_t* idToIndex, BitSet32 idBits, michael@0: int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) { michael@0: PointerCoords pointerCoords[MAX_POINTERS]; michael@0: PointerProperties pointerProperties[MAX_POINTERS]; michael@0: uint32_t pointerCount = 0; michael@0: while (!idBits.isEmpty()) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: uint32_t index = idToIndex[id]; michael@0: pointerProperties[pointerCount].copyFrom(properties[index]); michael@0: pointerCoords[pointerCount].copyFrom(coords[index]); michael@0: michael@0: if (changedId >= 0 && id == uint32_t(changedId)) { michael@0: action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; michael@0: } michael@0: michael@0: pointerCount += 1; michael@0: } michael@0: michael@0: ALOG_ASSERT(pointerCount != 0); michael@0: michael@0: if (changedId >= 0 && pointerCount == 1) { michael@0: // Replace initial down and final up action. michael@0: // We can compare the action without masking off the changed pointer index michael@0: // because we know the index is 0. michael@0: if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { michael@0: action = AMOTION_EVENT_ACTION_DOWN; michael@0: } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { michael@0: action = AMOTION_EVENT_ACTION_UP; michael@0: } else { michael@0: // Can't happen. michael@0: ALOG_ASSERT(false); michael@0: } michael@0: } michael@0: michael@0: NotifyMotionArgs args(when, getDeviceId(), source, policyFlags, michael@0: action, flags, metaState, buttonState, edgeFlags, michael@0: mViewport.displayId, pointerCount, pointerProperties, pointerCoords, michael@0: xPrecision, yPrecision, downTime); michael@0: getListener()->notifyMotion(&args); michael@0: } michael@0: michael@0: bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties, michael@0: const PointerCoords* inCoords, const uint32_t* inIdToIndex, michael@0: PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex, michael@0: BitSet32 idBits) const { michael@0: bool changed = false; michael@0: while (!idBits.isEmpty()) { michael@0: uint32_t id = idBits.clearFirstMarkedBit(); michael@0: uint32_t inIndex = inIdToIndex[id]; michael@0: uint32_t outIndex = outIdToIndex[id]; michael@0: michael@0: const PointerProperties& curInProperties = inProperties[inIndex]; michael@0: const PointerCoords& curInCoords = inCoords[inIndex]; michael@0: PointerProperties& curOutProperties = outProperties[outIndex]; michael@0: PointerCoords& curOutCoords = outCoords[outIndex]; michael@0: michael@0: if (curInProperties != curOutProperties) { michael@0: curOutProperties.copyFrom(curInProperties); michael@0: changed = true; michael@0: } michael@0: michael@0: if (curInCoords != curOutCoords) { michael@0: curOutCoords.copyFrom(curInCoords); michael@0: changed = true; michael@0: } michael@0: } michael@0: return changed; michael@0: } michael@0: michael@0: void TouchInputMapper::fadePointer() { michael@0: if (mPointerController != NULL) { michael@0: mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); michael@0: } michael@0: } michael@0: michael@0: bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) { michael@0: return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue michael@0: && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue; michael@0: } michael@0: michael@0: const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit( michael@0: int32_t x, int32_t y) { michael@0: size_t numVirtualKeys = mVirtualKeys.size(); michael@0: for (size_t i = 0; i < numVirtualKeys; i++) { michael@0: const VirtualKey& virtualKey = mVirtualKeys[i]; michael@0: michael@0: #if DEBUG_VIRTUAL_KEYS michael@0: ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " michael@0: "left=%d, top=%d, right=%d, bottom=%d", michael@0: x, y, michael@0: virtualKey.keyCode, virtualKey.scanCode, michael@0: virtualKey.hitLeft, virtualKey.hitTop, michael@0: virtualKey.hitRight, virtualKey.hitBottom); michael@0: #endif michael@0: michael@0: if (virtualKey.isHit(x, y)) { michael@0: return & virtualKey; michael@0: } michael@0: } michael@0: michael@0: return NULL; michael@0: } michael@0: michael@0: void TouchInputMapper::assignPointerIds() { michael@0: uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; michael@0: uint32_t lastPointerCount = mLastRawPointerData.pointerCount; michael@0: michael@0: mCurrentRawPointerData.clearIdBits(); michael@0: michael@0: if (currentPointerCount == 0) { michael@0: // No pointers to assign. michael@0: return; michael@0: } michael@0: michael@0: if (lastPointerCount == 0) { michael@0: // All pointers are new. michael@0: for (uint32_t i = 0; i < currentPointerCount; i++) { michael@0: uint32_t id = i; michael@0: mCurrentRawPointerData.pointers[i].id = id; michael@0: mCurrentRawPointerData.idToIndex[id] = i; michael@0: mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i)); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: if (currentPointerCount == 1 && lastPointerCount == 1 michael@0: && mCurrentRawPointerData.pointers[0].toolType michael@0: == mLastRawPointerData.pointers[0].toolType) { michael@0: // Only one pointer and no change in count so it must have the same id as before. michael@0: uint32_t id = mLastRawPointerData.pointers[0].id; michael@0: mCurrentRawPointerData.pointers[0].id = id; michael@0: mCurrentRawPointerData.idToIndex[id] = 0; michael@0: mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0)); michael@0: return; michael@0: } michael@0: michael@0: // General case. michael@0: // We build a heap of squared euclidean distances between current and last pointers michael@0: // associated with the current and last pointer indices. Then, we find the best michael@0: // match (by distance) for each current pointer. michael@0: // The pointers must have the same tool type but it is possible for them to michael@0: // transition from hovering to touching or vice-versa while retaining the same id. michael@0: PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; michael@0: michael@0: uint32_t heapSize = 0; michael@0: for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; michael@0: currentPointerIndex++) { michael@0: for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; michael@0: lastPointerIndex++) { michael@0: const RawPointerData::Pointer& currentPointer = michael@0: mCurrentRawPointerData.pointers[currentPointerIndex]; michael@0: const RawPointerData::Pointer& lastPointer = michael@0: mLastRawPointerData.pointers[lastPointerIndex]; michael@0: if (currentPointer.toolType == lastPointer.toolType) { michael@0: int64_t deltaX = currentPointer.x - lastPointer.x; michael@0: int64_t deltaY = currentPointer.y - lastPointer.y; michael@0: michael@0: uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); michael@0: michael@0: // Insert new element into the heap (sift up). michael@0: heap[heapSize].currentPointerIndex = currentPointerIndex; michael@0: heap[heapSize].lastPointerIndex = lastPointerIndex; michael@0: heap[heapSize].distance = distance; michael@0: heapSize += 1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Heapify michael@0: for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { michael@0: startIndex -= 1; michael@0: for (uint32_t parentIndex = startIndex; ;) { michael@0: uint32_t childIndex = parentIndex * 2 + 1; michael@0: if (childIndex >= heapSize) { michael@0: break; michael@0: } michael@0: michael@0: if (childIndex + 1 < heapSize michael@0: && heap[childIndex + 1].distance < heap[childIndex].distance) { michael@0: childIndex += 1; michael@0: } michael@0: michael@0: if (heap[parentIndex].distance <= heap[childIndex].distance) { michael@0: break; michael@0: } michael@0: michael@0: swap(heap[parentIndex], heap[childIndex]); michael@0: parentIndex = childIndex; michael@0: } michael@0: } michael@0: michael@0: #if DEBUG_POINTER_ASSIGNMENT michael@0: ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize); michael@0: for (size_t i = 0; i < heapSize; i++) { michael@0: ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", michael@0: i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, michael@0: heap[i].distance); michael@0: } michael@0: #endif michael@0: michael@0: // Pull matches out by increasing order of distance. michael@0: // To avoid reassigning pointers that have already been matched, the loop keeps track michael@0: // of which last and current pointers have been matched using the matchedXXXBits variables. michael@0: // It also tracks the used pointer id bits. michael@0: BitSet32 matchedLastBits(0); michael@0: BitSet32 matchedCurrentBits(0); michael@0: BitSet32 usedIdBits(0); michael@0: bool first = true; michael@0: for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) { michael@0: while (heapSize > 0) { michael@0: if (first) { michael@0: // The first time through the loop, we just consume the root element of michael@0: // the heap (the one with smallest distance). michael@0: first = false; michael@0: } else { michael@0: // Previous iterations consumed the root element of the heap. michael@0: // Pop root element off of the heap (sift down). michael@0: heap[0] = heap[heapSize]; michael@0: for (uint32_t parentIndex = 0; ;) { michael@0: uint32_t childIndex = parentIndex * 2 + 1; michael@0: if (childIndex >= heapSize) { michael@0: break; michael@0: } michael@0: michael@0: if (childIndex + 1 < heapSize michael@0: && heap[childIndex + 1].distance < heap[childIndex].distance) { michael@0: childIndex += 1; michael@0: } michael@0: michael@0: if (heap[parentIndex].distance <= heap[childIndex].distance) { michael@0: break; michael@0: } michael@0: michael@0: swap(heap[parentIndex], heap[childIndex]); michael@0: parentIndex = childIndex; michael@0: } michael@0: michael@0: #if DEBUG_POINTER_ASSIGNMENT michael@0: ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize); michael@0: for (size_t i = 0; i < heapSize; i++) { michael@0: ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", michael@0: i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, michael@0: heap[i].distance); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: heapSize -= 1; michael@0: michael@0: uint32_t currentPointerIndex = heap[0].currentPointerIndex; michael@0: if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched michael@0: michael@0: uint32_t lastPointerIndex = heap[0].lastPointerIndex; michael@0: if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched michael@0: michael@0: matchedCurrentBits.markBit(currentPointerIndex); michael@0: matchedLastBits.markBit(lastPointerIndex); michael@0: michael@0: uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id; michael@0: mCurrentRawPointerData.pointers[currentPointerIndex].id = id; michael@0: mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; michael@0: mCurrentRawPointerData.markIdBit(id, michael@0: mCurrentRawPointerData.isHovering(currentPointerIndex)); michael@0: usedIdBits.markBit(id); michael@0: michael@0: #if DEBUG_POINTER_ASSIGNMENT michael@0: ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", michael@0: lastPointerIndex, currentPointerIndex, id, heap[0].distance); michael@0: #endif michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // Assign fresh ids to pointers that were not matched in the process. michael@0: for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) { michael@0: uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit(); michael@0: uint32_t id = usedIdBits.markFirstUnmarkedBit(); michael@0: michael@0: mCurrentRawPointerData.pointers[currentPointerIndex].id = id; michael@0: mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; michael@0: mCurrentRawPointerData.markIdBit(id, michael@0: mCurrentRawPointerData.isHovering(currentPointerIndex)); michael@0: michael@0: #if DEBUG_POINTER_ASSIGNMENT michael@0: ALOGD("assignPointerIds - assigned: cur=%d, id=%d", michael@0: currentPointerIndex, id); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { michael@0: if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) { michael@0: return AKEY_STATE_VIRTUAL; michael@0: } michael@0: michael@0: size_t numVirtualKeys = mVirtualKeys.size(); michael@0: for (size_t i = 0; i < numVirtualKeys; i++) { michael@0: const VirtualKey& virtualKey = mVirtualKeys[i]; michael@0: if (virtualKey.keyCode == keyCode) { michael@0: return AKEY_STATE_UP; michael@0: } michael@0: } michael@0: michael@0: return AKEY_STATE_UNKNOWN; michael@0: } michael@0: michael@0: int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { michael@0: if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) { michael@0: return AKEY_STATE_VIRTUAL; michael@0: } michael@0: michael@0: size_t numVirtualKeys = mVirtualKeys.size(); michael@0: for (size_t i = 0; i < numVirtualKeys; i++) { michael@0: const VirtualKey& virtualKey = mVirtualKeys[i]; michael@0: if (virtualKey.scanCode == scanCode) { michael@0: return AKEY_STATE_UP; michael@0: } michael@0: } michael@0: michael@0: return AKEY_STATE_UNKNOWN; michael@0: } michael@0: michael@0: bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, michael@0: const int32_t* keyCodes, uint8_t* outFlags) { michael@0: size_t numVirtualKeys = mVirtualKeys.size(); michael@0: for (size_t i = 0; i < numVirtualKeys; i++) { michael@0: const VirtualKey& virtualKey = mVirtualKeys[i]; michael@0: michael@0: for (size_t i = 0; i < numCodes; i++) { michael@0: if (virtualKey.keyCode == keyCodes[i]) { michael@0: outFlags[i] = 1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: // --- SingleTouchInputMapper --- michael@0: michael@0: SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : michael@0: TouchInputMapper(device) { michael@0: } michael@0: michael@0: SingleTouchInputMapper::~SingleTouchInputMapper() { michael@0: } michael@0: michael@0: void SingleTouchInputMapper::reset(nsecs_t when) { michael@0: mSingleTouchMotionAccumulator.reset(getDevice()); michael@0: michael@0: TouchInputMapper::reset(when); michael@0: } michael@0: michael@0: void SingleTouchInputMapper::process(const RawEvent* rawEvent) { michael@0: TouchInputMapper::process(rawEvent); michael@0: michael@0: mSingleTouchMotionAccumulator.process(rawEvent); michael@0: } michael@0: michael@0: void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { michael@0: if (mTouchButtonAccumulator.isToolActive()) { michael@0: mCurrentRawPointerData.pointerCount = 1; michael@0: mCurrentRawPointerData.idToIndex[0] = 0; michael@0: michael@0: bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE michael@0: && (mTouchButtonAccumulator.isHovering() michael@0: || (mRawPointerAxes.pressure.valid michael@0: && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0)); michael@0: mCurrentRawPointerData.markIdBit(0, isHovering); michael@0: michael@0: RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0]; michael@0: outPointer.id = 0; michael@0: outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX(); michael@0: outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY(); michael@0: outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure(); michael@0: outPointer.touchMajor = 0; michael@0: outPointer.touchMinor = 0; michael@0: outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); michael@0: outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); michael@0: outPointer.orientation = 0; michael@0: outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance(); michael@0: outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX(); michael@0: outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY(); michael@0: outPointer.toolType = mTouchButtonAccumulator.getToolType(); michael@0: if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { michael@0: outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; michael@0: } michael@0: outPointer.isHovering = isHovering; michael@0: } michael@0: } michael@0: michael@0: void SingleTouchInputMapper::configureRawPointerAxes() { michael@0: TouchInputMapper::configureRawPointerAxes(); michael@0: michael@0: getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x); michael@0: getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y); michael@0: getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure); michael@0: getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor); michael@0: getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance); michael@0: getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX); michael@0: getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY); michael@0: } michael@0: michael@0: bool SingleTouchInputMapper::hasStylus() const { michael@0: return mTouchButtonAccumulator.hasStylus(); michael@0: } michael@0: michael@0: michael@0: // --- MultiTouchInputMapper --- michael@0: michael@0: MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : michael@0: TouchInputMapper(device) { michael@0: } michael@0: michael@0: MultiTouchInputMapper::~MultiTouchInputMapper() { michael@0: } michael@0: michael@0: void MultiTouchInputMapper::reset(nsecs_t when) { michael@0: mMultiTouchMotionAccumulator.reset(getDevice()); michael@0: michael@0: mPointerIdBits.clear(); michael@0: michael@0: TouchInputMapper::reset(when); michael@0: } michael@0: michael@0: void MultiTouchInputMapper::process(const RawEvent* rawEvent) { michael@0: TouchInputMapper::process(rawEvent); michael@0: michael@0: mMultiTouchMotionAccumulator.process(rawEvent); michael@0: } michael@0: michael@0: void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { michael@0: size_t inCount = mMultiTouchMotionAccumulator.getSlotCount(); michael@0: size_t outCount = 0; michael@0: BitSet32 newPointerIdBits; michael@0: michael@0: for (size_t inIndex = 0; inIndex < inCount; inIndex++) { michael@0: const MultiTouchMotionAccumulator::Slot* inSlot = michael@0: mMultiTouchMotionAccumulator.getSlot(inIndex); michael@0: if (!inSlot->isInUse()) { michael@0: continue; michael@0: } michael@0: michael@0: if (outCount >= MAX_POINTERS) { michael@0: #if DEBUG_POINTERS michael@0: ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; " michael@0: "ignoring the rest.", michael@0: getDeviceName().string(), MAX_POINTERS); michael@0: #endif michael@0: break; // too many fingers! michael@0: } michael@0: michael@0: RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount]; michael@0: outPointer.x = inSlot->getX(); michael@0: outPointer.y = inSlot->getY(); michael@0: outPointer.pressure = inSlot->getPressure(); michael@0: outPointer.touchMajor = inSlot->getTouchMajor(); michael@0: outPointer.touchMinor = inSlot->getTouchMinor(); michael@0: outPointer.toolMajor = inSlot->getToolMajor(); michael@0: outPointer.toolMinor = inSlot->getToolMinor(); michael@0: outPointer.orientation = inSlot->getOrientation(); michael@0: outPointer.distance = inSlot->getDistance(); michael@0: outPointer.tiltX = 0; michael@0: outPointer.tiltY = 0; michael@0: michael@0: outPointer.toolType = inSlot->getToolType(); michael@0: if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { michael@0: outPointer.toolType = mTouchButtonAccumulator.getToolType(); michael@0: if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { michael@0: outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; michael@0: } michael@0: } michael@0: michael@0: bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE michael@0: && (mTouchButtonAccumulator.isHovering() michael@0: || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0)); michael@0: outPointer.isHovering = isHovering; michael@0: michael@0: // Assign pointer id using tracking id if available. michael@0: if (*outHavePointerIds) { michael@0: int32_t trackingId = inSlot->getTrackingId(); michael@0: int32_t id = -1; michael@0: if (trackingId >= 0) { michael@0: for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) { michael@0: uint32_t n = idBits.clearFirstMarkedBit(); michael@0: if (mPointerTrackingIdMap[n] == trackingId) { michael@0: id = n; michael@0: } michael@0: } michael@0: michael@0: if (id < 0 && !mPointerIdBits.isFull()) { michael@0: id = mPointerIdBits.markFirstUnmarkedBit(); michael@0: mPointerTrackingIdMap[id] = trackingId; michael@0: } michael@0: } michael@0: if (id < 0) { michael@0: *outHavePointerIds = false; michael@0: mCurrentRawPointerData.clearIdBits(); michael@0: newPointerIdBits.clear(); michael@0: } else { michael@0: outPointer.id = id; michael@0: mCurrentRawPointerData.idToIndex[id] = outCount; michael@0: mCurrentRawPointerData.markIdBit(id, isHovering); michael@0: newPointerIdBits.markBit(id); michael@0: } michael@0: } michael@0: michael@0: outCount += 1; michael@0: } michael@0: michael@0: mCurrentRawPointerData.pointerCount = outCount; michael@0: mPointerIdBits = newPointerIdBits; michael@0: michael@0: mMultiTouchMotionAccumulator.finishSync(); michael@0: } michael@0: michael@0: void MultiTouchInputMapper::configureRawPointerAxes() { michael@0: TouchInputMapper::configureRawPointerAxes(); michael@0: michael@0: getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x); michael@0: getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y); michael@0: getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor); michael@0: getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor); michael@0: getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor); michael@0: getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor); michael@0: getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation); michael@0: getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure); michael@0: getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance); michael@0: getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId); michael@0: getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot); michael@0: michael@0: if (mRawPointerAxes.trackingId.valid michael@0: && mRawPointerAxes.slot.valid michael@0: && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) { michael@0: size_t slotCount = mRawPointerAxes.slot.maxValue + 1; michael@0: if (slotCount > MAX_SLOTS) { michael@0: ALOGW("MultiTouch Device %s reported %d slots but the framework " michael@0: "only supports a maximum of %d slots at this time.", michael@0: getDeviceName().string(), slotCount, MAX_SLOTS); michael@0: slotCount = MAX_SLOTS; michael@0: } michael@0: mMultiTouchMotionAccumulator.configure(getDevice(), michael@0: slotCount, true /*usingSlotsProtocol*/); michael@0: } else { michael@0: mMultiTouchMotionAccumulator.configure(getDevice(), michael@0: MAX_POINTERS, false /*usingSlotsProtocol*/); michael@0: } michael@0: } michael@0: michael@0: bool MultiTouchInputMapper::hasStylus() const { michael@0: return mMultiTouchMotionAccumulator.hasStylus() michael@0: || mTouchButtonAccumulator.hasStylus(); michael@0: } michael@0: michael@0: michael@0: // --- JoystickInputMapper --- michael@0: michael@0: JoystickInputMapper::JoystickInputMapper(InputDevice* device) : michael@0: InputMapper(device) { michael@0: } michael@0: michael@0: JoystickInputMapper::~JoystickInputMapper() { michael@0: } michael@0: michael@0: uint32_t JoystickInputMapper::getSources() { michael@0: return AINPUT_SOURCE_JOYSTICK; michael@0: } michael@0: michael@0: void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { michael@0: InputMapper::populateDeviceInfo(info); michael@0: michael@0: for (size_t i = 0; i < mAxes.size(); i++) { michael@0: const Axis& axis = mAxes.valueAt(i); michael@0: addMotionRange(axis.axisInfo.axis, axis, info); michael@0: michael@0: if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { michael@0: addMotionRange(axis.axisInfo.highAxis, axis, info); michael@0: michael@0: } michael@0: } michael@0: } michael@0: michael@0: void JoystickInputMapper::addMotionRange(int32_t axisId, const Axis& axis, michael@0: InputDeviceInfo* info) { michael@0: info->addMotionRange(axisId, AINPUT_SOURCE_JOYSTICK, michael@0: axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution); michael@0: /* In order to ease the transition for developers from using the old axes michael@0: * to the newer, more semantically correct axes, we'll continue to register michael@0: * the old axes as duplicates of their corresponding new ones. */ michael@0: int32_t compatAxis = getCompatAxis(axisId); michael@0: if (compatAxis >= 0) { michael@0: info->addMotionRange(compatAxis, AINPUT_SOURCE_JOYSTICK, michael@0: axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution); michael@0: } michael@0: } michael@0: michael@0: /* A mapping from axes the joystick actually has to the axes that should be michael@0: * artificially created for compatibility purposes. michael@0: * Returns -1 if no compatibility axis is needed. */ michael@0: int32_t JoystickInputMapper::getCompatAxis(int32_t axis) { michael@0: switch(axis) { michael@0: case AMOTION_EVENT_AXIS_LTRIGGER: michael@0: return AMOTION_EVENT_AXIS_BRAKE; michael@0: case AMOTION_EVENT_AXIS_RTRIGGER: michael@0: return AMOTION_EVENT_AXIS_GAS; michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: void JoystickInputMapper::dump(String8& dump) { michael@0: dump.append(INDENT2 "Joystick Input Mapper:\n"); michael@0: michael@0: dump.append(INDENT3 "Axes:\n"); michael@0: size_t numAxes = mAxes.size(); michael@0: for (size_t i = 0; i < numAxes; i++) { michael@0: const Axis& axis = mAxes.valueAt(i); michael@0: const char* label = getAxisLabel(axis.axisInfo.axis); michael@0: if (label) { michael@0: dump.appendFormat(INDENT4 "%s", label); michael@0: } else { michael@0: dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); michael@0: } michael@0: if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { michael@0: label = getAxisLabel(axis.axisInfo.highAxis); michael@0: if (label) { michael@0: dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); michael@0: } else { michael@0: dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, michael@0: axis.axisInfo.splitValue); michael@0: } michael@0: } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { michael@0: dump.append(" (invert)"); michael@0: } michael@0: michael@0: dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f, resolution=%0.5f\n", michael@0: axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution); michael@0: dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " michael@0: "highScale=%0.5f, highOffset=%0.5f\n", michael@0: axis.scale, axis.offset, axis.highScale, axis.highOffset); michael@0: dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " michael@0: "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", michael@0: mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, michael@0: axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution); michael@0: } michael@0: } michael@0: michael@0: void JoystickInputMapper::configure(nsecs_t when, michael@0: const InputReaderConfiguration* config, uint32_t changes) { michael@0: InputMapper::configure(when, config, changes); michael@0: michael@0: if (!changes) { // first time only michael@0: // Collect all axes. michael@0: for (int32_t abs = 0; abs <= ABS_MAX; abs++) { michael@0: if (!(getAbsAxisUsage(abs, getDevice()->getClasses()) michael@0: & INPUT_DEVICE_CLASS_JOYSTICK)) { michael@0: continue; // axis must be claimed by a different device michael@0: } michael@0: michael@0: RawAbsoluteAxisInfo rawAxisInfo; michael@0: getAbsoluteAxisInfo(abs, &rawAxisInfo); michael@0: if (rawAxisInfo.valid) { michael@0: // Map axis. michael@0: AxisInfo axisInfo; michael@0: bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); michael@0: if (!explicitlyMapped) { michael@0: // Axis is not explicitly mapped, will choose a generic axis later. michael@0: axisInfo.mode = AxisInfo::MODE_NORMAL; michael@0: axisInfo.axis = -1; michael@0: } michael@0: michael@0: // Apply flat override. michael@0: int32_t rawFlat = axisInfo.flatOverride < 0 michael@0: ? rawAxisInfo.flat : axisInfo.flatOverride; michael@0: michael@0: // Calculate scaling factors and limits. michael@0: Axis axis; michael@0: if (axisInfo.mode == AxisInfo::MODE_SPLIT) { michael@0: float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); michael@0: float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); michael@0: axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, michael@0: scale, 0.0f, highScale, 0.0f, michael@0: 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale, michael@0: rawAxisInfo.resolution * scale); michael@0: } else if (isCenteredAxis(axisInfo.axis)) { michael@0: float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); michael@0: float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; michael@0: axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, michael@0: scale, offset, scale, offset, michael@0: -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale, michael@0: rawAxisInfo.resolution * scale); michael@0: } else { michael@0: float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); michael@0: axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, michael@0: scale, 0.0f, scale, 0.0f, michael@0: 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale, michael@0: rawAxisInfo.resolution * scale); michael@0: } michael@0: michael@0: // To eliminate noise while the joystick is at rest, filter out small variations michael@0: // in axis values up front. michael@0: axis.filter = axis.flat * 0.25f; michael@0: michael@0: mAxes.add(abs, axis); michael@0: } michael@0: } michael@0: michael@0: // If there are too many axes, start dropping them. michael@0: // Prefer to keep explicitly mapped axes. michael@0: if (mAxes.size() > PointerCoords::MAX_AXES) { michael@0: ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.", michael@0: getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); michael@0: pruneAxes(true); michael@0: pruneAxes(false); michael@0: } michael@0: michael@0: // Assign generic axis ids to remaining axes. michael@0: int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; michael@0: size_t numAxes = mAxes.size(); michael@0: for (size_t i = 0; i < numAxes; i++) { michael@0: Axis& axis = mAxes.editValueAt(i); michael@0: if (axis.axisInfo.axis < 0) { michael@0: while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 michael@0: && haveAxis(nextGenericAxisId)) { michael@0: nextGenericAxisId += 1; michael@0: } michael@0: michael@0: if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { michael@0: axis.axisInfo.axis = nextGenericAxisId; michael@0: nextGenericAxisId += 1; michael@0: } else { michael@0: ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids " michael@0: "have already been assigned to other axes.", michael@0: getDeviceName().string(), mAxes.keyAt(i)); michael@0: mAxes.removeItemsAt(i--); michael@0: numAxes -= 1; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool JoystickInputMapper::haveAxis(int32_t axisId) { michael@0: size_t numAxes = mAxes.size(); michael@0: for (size_t i = 0; i < numAxes; i++) { michael@0: const Axis& axis = mAxes.valueAt(i); michael@0: if (axis.axisInfo.axis == axisId michael@0: || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT michael@0: && axis.axisInfo.highAxis == axisId)) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { michael@0: size_t i = mAxes.size(); michael@0: while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { michael@0: if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { michael@0: continue; michael@0: } michael@0: ALOGI("Discarding joystick '%s' axis %d because there are too many axes.", michael@0: getDeviceName().string(), mAxes.keyAt(i)); michael@0: mAxes.removeItemsAt(i); michael@0: } michael@0: } michael@0: michael@0: bool JoystickInputMapper::isCenteredAxis(int32_t axis) { michael@0: switch (axis) { michael@0: case AMOTION_EVENT_AXIS_X: michael@0: case AMOTION_EVENT_AXIS_Y: michael@0: case AMOTION_EVENT_AXIS_Z: michael@0: case AMOTION_EVENT_AXIS_RX: michael@0: case AMOTION_EVENT_AXIS_RY: michael@0: case AMOTION_EVENT_AXIS_RZ: michael@0: case AMOTION_EVENT_AXIS_HAT_X: michael@0: case AMOTION_EVENT_AXIS_HAT_Y: michael@0: case AMOTION_EVENT_AXIS_ORIENTATION: michael@0: case AMOTION_EVENT_AXIS_RUDDER: michael@0: case AMOTION_EVENT_AXIS_WHEEL: michael@0: return true; michael@0: default: michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: void JoystickInputMapper::reset(nsecs_t when) { michael@0: // Recenter all axes. michael@0: size_t numAxes = mAxes.size(); michael@0: for (size_t i = 0; i < numAxes; i++) { michael@0: Axis& axis = mAxes.editValueAt(i); michael@0: axis.resetValue(); michael@0: } michael@0: michael@0: InputMapper::reset(when); michael@0: } michael@0: michael@0: void JoystickInputMapper::process(const RawEvent* rawEvent) { michael@0: switch (rawEvent->type) { michael@0: case EV_ABS: { michael@0: ssize_t index = mAxes.indexOfKey(rawEvent->code); michael@0: if (index >= 0) { michael@0: Axis& axis = mAxes.editValueAt(index); michael@0: float newValue, highNewValue; michael@0: switch (axis.axisInfo.mode) { michael@0: case AxisInfo::MODE_INVERT: michael@0: newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) michael@0: * axis.scale + axis.offset; michael@0: highNewValue = 0.0f; michael@0: break; michael@0: case AxisInfo::MODE_SPLIT: michael@0: if (rawEvent->value < axis.axisInfo.splitValue) { michael@0: newValue = (axis.axisInfo.splitValue - rawEvent->value) michael@0: * axis.scale + axis.offset; michael@0: highNewValue = 0.0f; michael@0: } else if (rawEvent->value > axis.axisInfo.splitValue) { michael@0: newValue = 0.0f; michael@0: highNewValue = (rawEvent->value - axis.axisInfo.splitValue) michael@0: * axis.highScale + axis.highOffset; michael@0: } else { michael@0: newValue = 0.0f; michael@0: highNewValue = 0.0f; michael@0: } michael@0: break; michael@0: default: michael@0: newValue = rawEvent->value * axis.scale + axis.offset; michael@0: highNewValue = 0.0f; michael@0: break; michael@0: } michael@0: axis.newValue = newValue; michael@0: axis.highNewValue = highNewValue; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case EV_SYN: michael@0: switch (rawEvent->code) { michael@0: case SYN_REPORT: michael@0: sync(rawEvent->when, false /*force*/); michael@0: break; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: void JoystickInputMapper::sync(nsecs_t when, bool force) { michael@0: if (!filterAxes(force)) { michael@0: return; michael@0: } michael@0: michael@0: int32_t metaState = mContext->getGlobalMetaState(); michael@0: int32_t buttonState = 0; michael@0: michael@0: PointerProperties pointerProperties; michael@0: pointerProperties.clear(); michael@0: pointerProperties.id = 0; michael@0: pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; michael@0: michael@0: PointerCoords pointerCoords; michael@0: pointerCoords.clear(); michael@0: michael@0: size_t numAxes = mAxes.size(); michael@0: for (size_t i = 0; i < numAxes; i++) { michael@0: const Axis& axis = mAxes.valueAt(i); michael@0: setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.axis, axis.currentValue); michael@0: if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { michael@0: setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.highAxis, michael@0: axis.highCurrentValue); michael@0: } michael@0: } michael@0: michael@0: // Moving a joystick axis should not wake the device because joysticks can michael@0: // be fairly noisy even when not in use. On the other hand, pushing a gamepad michael@0: // button will likely wake the device. michael@0: // TODO: Use the input device configuration to control this behavior more finely. michael@0: uint32_t policyFlags = 0; michael@0: michael@0: NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, michael@0: AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, michael@0: ADISPLAY_ID_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, 0); michael@0: getListener()->notifyMotion(&args); michael@0: } michael@0: michael@0: void JoystickInputMapper::setPointerCoordsAxisValue(PointerCoords* pointerCoords, michael@0: int32_t axis, float value) { michael@0: pointerCoords->setAxisValue(axis, value); michael@0: /* In order to ease the transition for developers from using the old axes michael@0: * to the newer, more semantically correct axes, we'll continue to produce michael@0: * values for the old axes as mirrors of the value of their corresponding michael@0: * new axes. */ michael@0: int32_t compatAxis = getCompatAxis(axis); michael@0: if (compatAxis >= 0) { michael@0: pointerCoords->setAxisValue(compatAxis, value); michael@0: } michael@0: } michael@0: michael@0: bool JoystickInputMapper::filterAxes(bool force) { michael@0: bool atLeastOneSignificantChange = force; michael@0: size_t numAxes = mAxes.size(); michael@0: for (size_t i = 0; i < numAxes; i++) { michael@0: Axis& axis = mAxes.editValueAt(i); michael@0: if (force || hasValueChangedSignificantly(axis.filter, michael@0: axis.newValue, axis.currentValue, axis.min, axis.max)) { michael@0: axis.currentValue = axis.newValue; michael@0: atLeastOneSignificantChange = true; michael@0: } michael@0: if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { michael@0: if (force || hasValueChangedSignificantly(axis.filter, michael@0: axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { michael@0: axis.highCurrentValue = axis.highNewValue; michael@0: atLeastOneSignificantChange = true; michael@0: } michael@0: } michael@0: } michael@0: return atLeastOneSignificantChange; michael@0: } michael@0: michael@0: bool JoystickInputMapper::hasValueChangedSignificantly( michael@0: float filter, float newValue, float currentValue, float min, float max) { michael@0: if (newValue != currentValue) { michael@0: // Filter out small changes in value unless the value is converging on the axis michael@0: // bounds or center point. This is intended to reduce the amount of information michael@0: // sent to applications by particularly noisy joysticks (such as PS3). michael@0: if (fabs(newValue - currentValue) > filter michael@0: || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) michael@0: || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) michael@0: || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( michael@0: float filter, float newValue, float currentValue, float thresholdValue) { michael@0: float newDistance = fabs(newValue - thresholdValue); michael@0: if (newDistance < filter) { michael@0: float oldDistance = fabs(currentValue - thresholdValue); michael@0: if (newDistance < oldDistance) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: } // namespace android