| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include "Macros.h" | 
|  | 18 |  | 
|  | 19 | #include "TouchInputMapper.h" | 
|  | 20 |  | 
|  | 21 | #include "CursorButtonAccumulator.h" | 
|  | 22 | #include "CursorScrollAccumulator.h" | 
|  | 23 | #include "TouchButtonAccumulator.h" | 
|  | 24 | #include "TouchCursorInputMapperCommon.h" | 
|  | 25 |  | 
|  | 26 | namespace android { | 
|  | 27 |  | 
|  | 28 | // --- Constants --- | 
|  | 29 |  | 
|  | 30 | // Maximum amount of latency to add to touch events while waiting for data from an | 
|  | 31 | // external stylus. | 
|  | 32 | static constexpr nsecs_t EXTERNAL_STYLUS_DATA_TIMEOUT = ms2ns(72); | 
|  | 33 |  | 
|  | 34 | // Maximum amount of time to wait on touch data before pushing out new pressure data. | 
|  | 35 | static constexpr nsecs_t TOUCH_DATA_TIMEOUT = ms2ns(20); | 
|  | 36 |  | 
|  | 37 | // Artificial latency on synthetic events created from stylus data without corresponding touch | 
|  | 38 | // data. | 
|  | 39 | static constexpr nsecs_t STYLUS_DATA_LATENCY = ms2ns(10); | 
|  | 40 |  | 
|  | 41 | // --- Static Definitions --- | 
|  | 42 |  | 
|  | 43 | template <typename T> | 
|  | 44 | inline static void swap(T& a, T& b) { | 
|  | 45 | T temp = a; | 
|  | 46 | a = b; | 
|  | 47 | b = temp; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | static float calculateCommonVector(float a, float b) { | 
|  | 51 | if (a > 0 && b > 0) { | 
|  | 52 | return a < b ? a : b; | 
|  | 53 | } else if (a < 0 && b < 0) { | 
|  | 54 | return a > b ? a : b; | 
|  | 55 | } else { | 
|  | 56 | return 0; | 
|  | 57 | } | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | inline static float distance(float x1, float y1, float x2, float y2) { | 
|  | 61 | return hypotf(x1 - x2, y1 - y2); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | inline static int32_t signExtendNybble(int32_t value) { | 
|  | 65 | return value >= 8 ? value - 16 : value; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | // --- RawPointerAxes --- | 
|  | 69 |  | 
|  | 70 | RawPointerAxes::RawPointerAxes() { | 
|  | 71 | clear(); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | void RawPointerAxes::clear() { | 
|  | 75 | x.clear(); | 
|  | 76 | y.clear(); | 
|  | 77 | pressure.clear(); | 
|  | 78 | touchMajor.clear(); | 
|  | 79 | touchMinor.clear(); | 
|  | 80 | toolMajor.clear(); | 
|  | 81 | toolMinor.clear(); | 
|  | 82 | orientation.clear(); | 
|  | 83 | distance.clear(); | 
|  | 84 | tiltX.clear(); | 
|  | 85 | tiltY.clear(); | 
|  | 86 | trackingId.clear(); | 
|  | 87 | slot.clear(); | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | // --- RawPointerData --- | 
|  | 91 |  | 
|  | 92 | RawPointerData::RawPointerData() { | 
|  | 93 | clear(); | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | void RawPointerData::clear() { | 
|  | 97 | pointerCount = 0; | 
|  | 98 | clearIdBits(); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | void RawPointerData::copyFrom(const RawPointerData& other) { | 
|  | 102 | pointerCount = other.pointerCount; | 
|  | 103 | hoveringIdBits = other.hoveringIdBits; | 
|  | 104 | touchingIdBits = other.touchingIdBits; | 
|  | 105 |  | 
|  | 106 | for (uint32_t i = 0; i < pointerCount; i++) { | 
|  | 107 | pointers[i] = other.pointers[i]; | 
|  | 108 |  | 
|  | 109 | int id = pointers[i].id; | 
|  | 110 | idToIndex[id] = other.idToIndex[id]; | 
|  | 111 | } | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const { | 
|  | 115 | float x = 0, y = 0; | 
|  | 116 | uint32_t count = touchingIdBits.count(); | 
|  | 117 | if (count) { | 
|  | 118 | for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty();) { | 
|  | 119 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 120 | const Pointer& pointer = pointerForId(id); | 
|  | 121 | x += pointer.x; | 
|  | 122 | y += pointer.y; | 
|  | 123 | } | 
|  | 124 | x /= count; | 
|  | 125 | y /= count; | 
|  | 126 | } | 
|  | 127 | *outX = x; | 
|  | 128 | *outY = y; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | // --- CookedPointerData --- | 
|  | 132 |  | 
|  | 133 | CookedPointerData::CookedPointerData() { | 
|  | 134 | clear(); | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | void CookedPointerData::clear() { | 
|  | 138 | pointerCount = 0; | 
|  | 139 | hoveringIdBits.clear(); | 
|  | 140 | touchingIdBits.clear(); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | void CookedPointerData::copyFrom(const CookedPointerData& other) { | 
|  | 144 | pointerCount = other.pointerCount; | 
|  | 145 | hoveringIdBits = other.hoveringIdBits; | 
|  | 146 | touchingIdBits = other.touchingIdBits; | 
|  | 147 |  | 
|  | 148 | for (uint32_t i = 0; i < pointerCount; i++) { | 
|  | 149 | pointerProperties[i].copyFrom(other.pointerProperties[i]); | 
|  | 150 | pointerCoords[i].copyFrom(other.pointerCoords[i]); | 
|  | 151 |  | 
|  | 152 | int id = pointerProperties[i].id; | 
|  | 153 | idToIndex[id] = other.idToIndex[id]; | 
|  | 154 | } | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | // --- TouchInputMapper --- | 
|  | 158 |  | 
|  | 159 | TouchInputMapper::TouchInputMapper(InputDevice* device) | 
|  | 160 | : InputMapper(device), | 
|  | 161 | mSource(0), | 
|  | 162 | mDeviceMode(DEVICE_MODE_DISABLED), | 
|  | 163 | mSurfaceWidth(-1), | 
|  | 164 | mSurfaceHeight(-1), | 
|  | 165 | mSurfaceLeft(0), | 
|  | 166 | mSurfaceTop(0), | 
|  | 167 | mPhysicalWidth(-1), | 
|  | 168 | mPhysicalHeight(-1), | 
|  | 169 | mPhysicalLeft(0), | 
|  | 170 | mPhysicalTop(0), | 
|  | 171 | mSurfaceOrientation(DISPLAY_ORIENTATION_0) {} | 
|  | 172 |  | 
|  | 173 | TouchInputMapper::~TouchInputMapper() {} | 
|  | 174 |  | 
|  | 175 | uint32_t TouchInputMapper::getSources() { | 
|  | 176 | return mSource; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { | 
|  | 180 | InputMapper::populateDeviceInfo(info); | 
|  | 181 |  | 
|  | 182 | if (mDeviceMode != DEVICE_MODE_DISABLED) { | 
|  | 183 | info->addMotionRange(mOrientedRanges.x); | 
|  | 184 | info->addMotionRange(mOrientedRanges.y); | 
|  | 185 | info->addMotionRange(mOrientedRanges.pressure); | 
|  | 186 |  | 
|  | 187 | if (mOrientedRanges.haveSize) { | 
|  | 188 | info->addMotionRange(mOrientedRanges.size); | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | if (mOrientedRanges.haveTouchSize) { | 
|  | 192 | info->addMotionRange(mOrientedRanges.touchMajor); | 
|  | 193 | info->addMotionRange(mOrientedRanges.touchMinor); | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | if (mOrientedRanges.haveToolSize) { | 
|  | 197 | info->addMotionRange(mOrientedRanges.toolMajor); | 
|  | 198 | info->addMotionRange(mOrientedRanges.toolMinor); | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | if (mOrientedRanges.haveOrientation) { | 
|  | 202 | info->addMotionRange(mOrientedRanges.orientation); | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | if (mOrientedRanges.haveDistance) { | 
|  | 206 | info->addMotionRange(mOrientedRanges.distance); | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | if (mOrientedRanges.haveTilt) { | 
|  | 210 | info->addMotionRange(mOrientedRanges.tilt); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { | 
|  | 214 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, | 
|  | 215 | 0.0f); | 
|  | 216 | } | 
|  | 217 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { | 
|  | 218 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, | 
|  | 219 | 0.0f); | 
|  | 220 | } | 
|  | 221 | if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) { | 
|  | 222 | const InputDeviceInfo::MotionRange& x = mOrientedRanges.x; | 
|  | 223 | const InputDeviceInfo::MotionRange& y = mOrientedRanges.y; | 
|  | 224 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat, | 
|  | 225 | x.fuzz, x.resolution); | 
|  | 226 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat, | 
|  | 227 | y.fuzz, y.resolution); | 
|  | 228 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat, | 
|  | 229 | x.fuzz, x.resolution); | 
|  | 230 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat, | 
|  | 231 | y.fuzz, y.resolution); | 
|  | 232 | } | 
|  | 233 | info->setButtonUnderPad(mParameters.hasButtonUnderPad); | 
|  | 234 | } | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | void TouchInputMapper::dump(std::string& dump) { | 
|  | 238 | dump += StringPrintf(INDENT2 "Touch Input Mapper (mode - %s):\n", modeToString(mDeviceMode)); | 
|  | 239 | dumpParameters(dump); | 
|  | 240 | dumpVirtualKeys(dump); | 
|  | 241 | dumpRawPointerAxes(dump); | 
|  | 242 | dumpCalibration(dump); | 
|  | 243 | dumpAffineTransformation(dump); | 
|  | 244 | dumpSurface(dump); | 
|  | 245 |  | 
|  | 246 | dump += StringPrintf(INDENT3 "Translation and Scaling Factors:\n"); | 
|  | 247 | dump += StringPrintf(INDENT4 "XTranslate: %0.3f\n", mXTranslate); | 
|  | 248 | dump += StringPrintf(INDENT4 "YTranslate: %0.3f\n", mYTranslate); | 
|  | 249 | dump += StringPrintf(INDENT4 "XScale: %0.3f\n", mXScale); | 
|  | 250 | dump += StringPrintf(INDENT4 "YScale: %0.3f\n", mYScale); | 
|  | 251 | dump += StringPrintf(INDENT4 "XPrecision: %0.3f\n", mXPrecision); | 
|  | 252 | dump += StringPrintf(INDENT4 "YPrecision: %0.3f\n", mYPrecision); | 
|  | 253 | dump += StringPrintf(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale); | 
|  | 254 | dump += StringPrintf(INDENT4 "PressureScale: %0.3f\n", mPressureScale); | 
|  | 255 | dump += StringPrintf(INDENT4 "SizeScale: %0.3f\n", mSizeScale); | 
|  | 256 | dump += StringPrintf(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale); | 
|  | 257 | dump += StringPrintf(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale); | 
|  | 258 | dump += StringPrintf(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt)); | 
|  | 259 | dump += StringPrintf(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter); | 
|  | 260 | dump += StringPrintf(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale); | 
|  | 261 | dump += StringPrintf(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter); | 
|  | 262 | dump += StringPrintf(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale); | 
|  | 263 |  | 
|  | 264 | dump += StringPrintf(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState); | 
|  | 265 | dump += StringPrintf(INDENT3 "Last Raw Touch: pointerCount=%d\n", | 
|  | 266 | mLastRawState.rawPointerData.pointerCount); | 
|  | 267 | for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) { | 
|  | 268 | const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i]; | 
|  | 269 | dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " | 
|  | 270 | "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " | 
|  | 271 | "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, " | 
|  | 272 | "toolType=%d, isHovering=%s\n", | 
|  | 273 | i, pointer.id, pointer.x, pointer.y, pointer.pressure, | 
|  | 274 | pointer.touchMajor, pointer.touchMinor, pointer.toolMajor, | 
|  | 275 | pointer.toolMinor, pointer.orientation, pointer.tiltX, pointer.tiltY, | 
|  | 276 | pointer.distance, pointer.toolType, toString(pointer.isHovering)); | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | dump += StringPrintf(INDENT3 "Last Cooked Button State: 0x%08x\n", | 
|  | 280 | mLastCookedState.buttonState); | 
|  | 281 | dump += StringPrintf(INDENT3 "Last Cooked Touch: pointerCount=%d\n", | 
|  | 282 | mLastCookedState.cookedPointerData.pointerCount); | 
|  | 283 | for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) { | 
|  | 284 | const PointerProperties& pointerProperties = | 
|  | 285 | mLastCookedState.cookedPointerData.pointerProperties[i]; | 
|  | 286 | const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i]; | 
|  | 287 | dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, " | 
|  | 288 | "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, " | 
|  | 289 | "toolMinor=%0.3f, " | 
|  | 290 | "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, " | 
|  | 291 | "toolType=%d, isHovering=%s\n", | 
|  | 292 | i, pointerProperties.id, pointerCoords.getX(), pointerCoords.getY(), | 
|  | 293 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 294 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 295 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 296 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 297 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 298 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), | 
|  | 299 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT), | 
|  | 300 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), | 
|  | 301 | pointerProperties.toolType, | 
|  | 302 | toString(mLastCookedState.cookedPointerData.isHovering(i))); | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | dump += INDENT3 "Stylus Fusion:\n"; | 
|  | 306 | dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n", | 
|  | 307 | toString(mExternalStylusConnected)); | 
|  | 308 | dump += StringPrintf(INDENT4 "External Stylus ID: %" PRId64 "\n", mExternalStylusId); | 
|  | 309 | dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n", | 
|  | 310 | mExternalStylusFusionTimeout); | 
|  | 311 | dump += INDENT3 "External Stylus State:\n"; | 
|  | 312 | dumpStylusState(dump, mExternalStylusState); | 
|  | 313 |  | 
|  | 314 | if (mDeviceMode == DEVICE_MODE_POINTER) { | 
|  | 315 | dump += StringPrintf(INDENT3 "Pointer Gesture Detector:\n"); | 
|  | 316 | dump += StringPrintf(INDENT4 "XMovementScale: %0.3f\n", mPointerXMovementScale); | 
|  | 317 | dump += StringPrintf(INDENT4 "YMovementScale: %0.3f\n", mPointerYMovementScale); | 
|  | 318 | dump += StringPrintf(INDENT4 "XZoomScale: %0.3f\n", mPointerXZoomScale); | 
|  | 319 | dump += StringPrintf(INDENT4 "YZoomScale: %0.3f\n", mPointerYZoomScale); | 
|  | 320 | dump += StringPrintf(INDENT4 "MaxSwipeWidth: %f\n", mPointerGestureMaxSwipeWidth); | 
|  | 321 | } | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | const char* TouchInputMapper::modeToString(DeviceMode deviceMode) { | 
|  | 325 | switch (deviceMode) { | 
|  | 326 | case DEVICE_MODE_DISABLED: | 
|  | 327 | return "disabled"; | 
|  | 328 | case DEVICE_MODE_DIRECT: | 
|  | 329 | return "direct"; | 
|  | 330 | case DEVICE_MODE_UNSCALED: | 
|  | 331 | return "unscaled"; | 
|  | 332 | case DEVICE_MODE_NAVIGATION: | 
|  | 333 | return "navigation"; | 
|  | 334 | case DEVICE_MODE_POINTER: | 
|  | 335 | return "pointer"; | 
|  | 336 | } | 
|  | 337 | return "unknown"; | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | void TouchInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config, | 
|  | 341 | uint32_t changes) { | 
|  | 342 | InputMapper::configure(when, config, changes); | 
|  | 343 |  | 
|  | 344 | mConfig = *config; | 
|  | 345 |  | 
|  | 346 | if (!changes) { // first time only | 
|  | 347 | // Configure basic parameters. | 
|  | 348 | configureParameters(); | 
|  | 349 |  | 
|  | 350 | // Configure common accumulators. | 
|  | 351 | mCursorScrollAccumulator.configure(getDevice()); | 
|  | 352 | mTouchButtonAccumulator.configure(getDevice()); | 
|  | 353 |  | 
|  | 354 | // Configure absolute axis information. | 
|  | 355 | configureRawPointerAxes(); | 
|  | 356 |  | 
|  | 357 | // Prepare input device calibration. | 
|  | 358 | parseCalibration(); | 
|  | 359 | resolveCalibration(); | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) { | 
|  | 363 | // Update location calibration to reflect current settings | 
|  | 364 | updateAffineTransformation(); | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { | 
|  | 368 | // Update pointer speed. | 
|  | 369 | mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters); | 
|  | 370 | mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); | 
|  | 371 | mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | bool resetNeeded = false; | 
|  | 375 | if (!changes || | 
|  | 376 | (changes & | 
|  | 377 | (InputReaderConfiguration::CHANGE_DISPLAY_INFO | | 
|  | 378 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT | | 
|  | 379 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES | | 
|  | 380 | InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) { | 
|  | 381 | // Configure device sources, surface dimensions, orientation and | 
|  | 382 | // scaling factors. | 
|  | 383 | configureSurface(when, &resetNeeded); | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | if (changes && resetNeeded) { | 
|  | 387 | // Send reset, unless this is the first time the device has been configured, | 
|  | 388 | // in which case the reader will call reset itself after all mappers are ready. | 
|  | 389 | getDevice()->notifyReset(when); | 
|  | 390 | } | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | void TouchInputMapper::resolveExternalStylusPresence() { | 
|  | 394 | std::vector<InputDeviceInfo> devices; | 
|  | 395 | mContext->getExternalStylusDevices(devices); | 
|  | 396 | mExternalStylusConnected = !devices.empty(); | 
|  | 397 |  | 
|  | 398 | if (!mExternalStylusConnected) { | 
|  | 399 | resetExternalStylus(); | 
|  | 400 | } | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | void TouchInputMapper::configureParameters() { | 
|  | 404 | // Use the pointer presentation mode for devices that do not support distinct | 
|  | 405 | // multitouch.  The spot-based presentation relies on being able to accurately | 
|  | 406 | // locate two or more fingers on the touch pad. | 
|  | 407 | mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT) | 
|  | 408 | ? Parameters::GESTURE_MODE_SINGLE_TOUCH | 
|  | 409 | : Parameters::GESTURE_MODE_MULTI_TOUCH; | 
|  | 410 |  | 
|  | 411 | String8 gestureModeString; | 
|  | 412 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), | 
|  | 413 | gestureModeString)) { | 
|  | 414 | if (gestureModeString == "single-touch") { | 
|  | 415 | mParameters.gestureMode = Parameters::GESTURE_MODE_SINGLE_TOUCH; | 
|  | 416 | } else if (gestureModeString == "multi-touch") { | 
|  | 417 | mParameters.gestureMode = Parameters::GESTURE_MODE_MULTI_TOUCH; | 
|  | 418 | } else if (gestureModeString != "default") { | 
|  | 419 | ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); | 
|  | 420 | } | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) { | 
|  | 424 | // The device is a touch screen. | 
|  | 425 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; | 
|  | 426 | } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) { | 
|  | 427 | // The device is a pointing device like a track pad. | 
|  | 428 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; | 
|  | 429 | } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X) || | 
|  | 430 | getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { | 
|  | 431 | // The device is a cursor device with a touch pad attached. | 
|  | 432 | // By default don't use the touch pad to move the pointer. | 
|  | 433 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; | 
|  | 434 | } else { | 
|  | 435 | // The device is a touch pad of unknown purpose. | 
|  | 436 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; | 
|  | 437 | } | 
|  | 438 |  | 
|  | 439 | mParameters.hasButtonUnderPad = | 
|  | 440 | getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_BUTTONPAD); | 
|  | 441 |  | 
|  | 442 | String8 deviceTypeString; | 
|  | 443 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), | 
|  | 444 | deviceTypeString)) { | 
|  | 445 | if (deviceTypeString == "touchScreen") { | 
|  | 446 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; | 
|  | 447 | } else if (deviceTypeString == "touchPad") { | 
|  | 448 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; | 
|  | 449 | } else if (deviceTypeString == "touchNavigation") { | 
|  | 450 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_NAVIGATION; | 
|  | 451 | } else if (deviceTypeString == "pointer") { | 
|  | 452 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; | 
|  | 453 | } else if (deviceTypeString != "default") { | 
|  | 454 | ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); | 
|  | 455 | } | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; | 
|  | 459 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), | 
|  | 460 | mParameters.orientationAware); | 
|  | 461 |  | 
|  | 462 | mParameters.hasAssociatedDisplay = false; | 
|  | 463 | mParameters.associatedDisplayIsExternal = false; | 
|  | 464 | if (mParameters.orientationAware || | 
|  | 465 | mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN || | 
|  | 466 | mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { | 
|  | 467 | mParameters.hasAssociatedDisplay = true; | 
|  | 468 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) { | 
|  | 469 | mParameters.associatedDisplayIsExternal = getDevice()->isExternal(); | 
|  | 470 | String8 uniqueDisplayId; | 
|  | 471 | getDevice()->getConfiguration().tryGetProperty(String8("touch.displayId"), | 
|  | 472 | uniqueDisplayId); | 
|  | 473 | mParameters.uniqueDisplayId = uniqueDisplayId.c_str(); | 
|  | 474 | } | 
|  | 475 | } | 
|  | 476 | if (getDevice()->getAssociatedDisplayPort()) { | 
|  | 477 | mParameters.hasAssociatedDisplay = true; | 
|  | 478 | } | 
|  | 479 |  | 
|  | 480 | // Initial downs on external touch devices should wake the device. | 
|  | 481 | // Normally we don't do this for internal touch screens to prevent them from waking | 
|  | 482 | // up in your pocket but you can enable it using the input device configuration. | 
|  | 483 | mParameters.wake = getDevice()->isExternal(); | 
|  | 484 | getDevice()->getConfiguration().tryGetProperty(String8("touch.wake"), mParameters.wake); | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | void TouchInputMapper::dumpParameters(std::string& dump) { | 
|  | 488 | dump += INDENT3 "Parameters:\n"; | 
|  | 489 |  | 
|  | 490 | switch (mParameters.gestureMode) { | 
|  | 491 | case Parameters::GESTURE_MODE_SINGLE_TOUCH: | 
|  | 492 | dump += INDENT4 "GestureMode: single-touch\n"; | 
|  | 493 | break; | 
|  | 494 | case Parameters::GESTURE_MODE_MULTI_TOUCH: | 
|  | 495 | dump += INDENT4 "GestureMode: multi-touch\n"; | 
|  | 496 | break; | 
|  | 497 | default: | 
|  | 498 | assert(false); | 
|  | 499 | } | 
|  | 500 |  | 
|  | 501 | switch (mParameters.deviceType) { | 
|  | 502 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: | 
|  | 503 | dump += INDENT4 "DeviceType: touchScreen\n"; | 
|  | 504 | break; | 
|  | 505 | case Parameters::DEVICE_TYPE_TOUCH_PAD: | 
|  | 506 | dump += INDENT4 "DeviceType: touchPad\n"; | 
|  | 507 | break; | 
|  | 508 | case Parameters::DEVICE_TYPE_TOUCH_NAVIGATION: | 
|  | 509 | dump += INDENT4 "DeviceType: touchNavigation\n"; | 
|  | 510 | break; | 
|  | 511 | case Parameters::DEVICE_TYPE_POINTER: | 
|  | 512 | dump += INDENT4 "DeviceType: pointer\n"; | 
|  | 513 | break; | 
|  | 514 | default: | 
|  | 515 | ALOG_ASSERT(false); | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, " | 
|  | 519 | "displayId='%s'\n", | 
|  | 520 | toString(mParameters.hasAssociatedDisplay), | 
|  | 521 | toString(mParameters.associatedDisplayIsExternal), | 
|  | 522 | mParameters.uniqueDisplayId.c_str()); | 
|  | 523 | dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware)); | 
|  | 524 | } | 
|  | 525 |  | 
|  | 526 | void TouchInputMapper::configureRawPointerAxes() { | 
|  | 527 | mRawPointerAxes.clear(); | 
|  | 528 | } | 
|  | 529 |  | 
|  | 530 | void TouchInputMapper::dumpRawPointerAxes(std::string& dump) { | 
|  | 531 | dump += INDENT3 "Raw Touch Axes:\n"; | 
|  | 532 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X"); | 
|  | 533 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y"); | 
|  | 534 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure"); | 
|  | 535 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor"); | 
|  | 536 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor"); | 
|  | 537 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor"); | 
|  | 538 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor"); | 
|  | 539 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation"); | 
|  | 540 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance"); | 
|  | 541 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX"); | 
|  | 542 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY"); | 
|  | 543 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId"); | 
|  | 544 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot"); | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | bool TouchInputMapper::hasExternalStylus() const { | 
|  | 548 | return mExternalStylusConnected; | 
|  | 549 | } | 
|  | 550 |  | 
|  | 551 | /** | 
|  | 552 | * Determine which DisplayViewport to use. | 
|  | 553 | * 1. If display port is specified, return the matching viewport. If matching viewport not | 
|  | 554 | * found, then return. | 
|  | 555 | * 2. If a device has associated display, get the matching viewport by either unique id or by | 
|  | 556 | * the display type (internal or external). | 
|  | 557 | * 3. Otherwise, use a non-display viewport. | 
|  | 558 | */ | 
|  | 559 | std::optional<DisplayViewport> TouchInputMapper::findViewport() { | 
|  | 560 | if (mParameters.hasAssociatedDisplay) { | 
|  | 561 | const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort(); | 
|  | 562 | if (displayPort) { | 
|  | 563 | // Find the viewport that contains the same port | 
|  | 564 | return mDevice->getAssociatedViewport(); | 
|  | 565 | } | 
|  | 566 |  | 
|  | 567 | // Check if uniqueDisplayId is specified in idc file. | 
|  | 568 | if (!mParameters.uniqueDisplayId.empty()) { | 
|  | 569 | return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId); | 
|  | 570 | } | 
|  | 571 |  | 
|  | 572 | ViewportType viewportTypeToUse; | 
|  | 573 | if (mParameters.associatedDisplayIsExternal) { | 
|  | 574 | viewportTypeToUse = ViewportType::VIEWPORT_EXTERNAL; | 
|  | 575 | } else { | 
|  | 576 | viewportTypeToUse = ViewportType::VIEWPORT_INTERNAL; | 
|  | 577 | } | 
|  | 578 |  | 
|  | 579 | std::optional<DisplayViewport> viewport = | 
|  | 580 | mConfig.getDisplayViewportByType(viewportTypeToUse); | 
|  | 581 | if (!viewport && viewportTypeToUse == ViewportType::VIEWPORT_EXTERNAL) { | 
|  | 582 | ALOGW("Input device %s should be associated with external display, " | 
|  | 583 | "fallback to internal one for the external viewport is not found.", | 
|  | 584 | getDeviceName().c_str()); | 
|  | 585 | viewport = mConfig.getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL); | 
|  | 586 | } | 
|  | 587 |  | 
|  | 588 | return viewport; | 
|  | 589 | } | 
|  | 590 |  | 
|  | 591 | // No associated display, return a non-display viewport. | 
|  | 592 | DisplayViewport newViewport; | 
|  | 593 | // Raw width and height in the natural orientation. | 
|  | 594 | int32_t rawWidth = mRawPointerAxes.getRawWidth(); | 
|  | 595 | int32_t rawHeight = mRawPointerAxes.getRawHeight(); | 
|  | 596 | newViewport.setNonDisplayViewport(rawWidth, rawHeight); | 
|  | 597 | return std::make_optional(newViewport); | 
|  | 598 | } | 
|  | 599 |  | 
|  | 600 | void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) { | 
|  | 601 | int32_t oldDeviceMode = mDeviceMode; | 
|  | 602 |  | 
|  | 603 | resolveExternalStylusPresence(); | 
|  | 604 |  | 
|  | 605 | // Determine device mode. | 
|  | 606 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER && | 
|  | 607 | mConfig.pointerGesturesEnabled) { | 
|  | 608 | mSource = AINPUT_SOURCE_MOUSE; | 
|  | 609 | mDeviceMode = DEVICE_MODE_POINTER; | 
|  | 610 | if (hasStylus()) { | 
|  | 611 | mSource |= AINPUT_SOURCE_STYLUS; | 
|  | 612 | } | 
|  | 613 | } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN && | 
|  | 614 | mParameters.hasAssociatedDisplay) { | 
|  | 615 | mSource = AINPUT_SOURCE_TOUCHSCREEN; | 
|  | 616 | mDeviceMode = DEVICE_MODE_DIRECT; | 
|  | 617 | if (hasStylus()) { | 
|  | 618 | mSource |= AINPUT_SOURCE_STYLUS; | 
|  | 619 | } | 
|  | 620 | if (hasExternalStylus()) { | 
|  | 621 | mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS; | 
|  | 622 | } | 
|  | 623 | } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_NAVIGATION) { | 
|  | 624 | mSource = AINPUT_SOURCE_TOUCH_NAVIGATION; | 
|  | 625 | mDeviceMode = DEVICE_MODE_NAVIGATION; | 
|  | 626 | } else { | 
|  | 627 | mSource = AINPUT_SOURCE_TOUCHPAD; | 
|  | 628 | mDeviceMode = DEVICE_MODE_UNSCALED; | 
|  | 629 | } | 
|  | 630 |  | 
|  | 631 | // Ensure we have valid X and Y axes. | 
|  | 632 | if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) { | 
|  | 633 | ALOGW("Touch device '%s' did not report support for X or Y axis!  " | 
|  | 634 | "The device will be inoperable.", | 
|  | 635 | getDeviceName().c_str()); | 
|  | 636 | mDeviceMode = DEVICE_MODE_DISABLED; | 
|  | 637 | return; | 
|  | 638 | } | 
|  | 639 |  | 
|  | 640 | // Get associated display dimensions. | 
|  | 641 | std::optional<DisplayViewport> newViewport = findViewport(); | 
|  | 642 | if (!newViewport) { | 
|  | 643 | ALOGI("Touch device '%s' could not query the properties of its associated " | 
|  | 644 | "display.  The device will be inoperable until the display size " | 
|  | 645 | "becomes available.", | 
|  | 646 | getDeviceName().c_str()); | 
|  | 647 | mDeviceMode = DEVICE_MODE_DISABLED; | 
|  | 648 | return; | 
|  | 649 | } | 
|  | 650 |  | 
|  | 651 | // Raw width and height in the natural orientation. | 
|  | 652 | int32_t rawWidth = mRawPointerAxes.getRawWidth(); | 
|  | 653 | int32_t rawHeight = mRawPointerAxes.getRawHeight(); | 
|  | 654 |  | 
|  | 655 | bool viewportChanged = mViewport != *newViewport; | 
|  | 656 | if (viewportChanged) { | 
|  | 657 | mViewport = *newViewport; | 
|  | 658 |  | 
|  | 659 | if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) { | 
|  | 660 | // Convert rotated viewport to natural surface coordinates. | 
|  | 661 | int32_t naturalLogicalWidth, naturalLogicalHeight; | 
|  | 662 | int32_t naturalPhysicalWidth, naturalPhysicalHeight; | 
|  | 663 | int32_t naturalPhysicalLeft, naturalPhysicalTop; | 
|  | 664 | int32_t naturalDeviceWidth, naturalDeviceHeight; | 
|  | 665 | switch (mViewport.orientation) { | 
|  | 666 | case DISPLAY_ORIENTATION_90: | 
|  | 667 | naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop; | 
|  | 668 | naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft; | 
|  | 669 | naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop; | 
|  | 670 | naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft; | 
|  | 671 | naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom; | 
|  | 672 | naturalPhysicalTop = mViewport.physicalLeft; | 
|  | 673 | naturalDeviceWidth = mViewport.deviceHeight; | 
|  | 674 | naturalDeviceHeight = mViewport.deviceWidth; | 
|  | 675 | break; | 
|  | 676 | case DISPLAY_ORIENTATION_180: | 
|  | 677 | naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft; | 
|  | 678 | naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop; | 
|  | 679 | naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft; | 
|  | 680 | naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop; | 
|  | 681 | naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight; | 
|  | 682 | naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom; | 
|  | 683 | naturalDeviceWidth = mViewport.deviceWidth; | 
|  | 684 | naturalDeviceHeight = mViewport.deviceHeight; | 
|  | 685 | break; | 
|  | 686 | case DISPLAY_ORIENTATION_270: | 
|  | 687 | naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop; | 
|  | 688 | naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft; | 
|  | 689 | naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop; | 
|  | 690 | naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft; | 
|  | 691 | naturalPhysicalLeft = mViewport.physicalTop; | 
|  | 692 | naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight; | 
|  | 693 | naturalDeviceWidth = mViewport.deviceHeight; | 
|  | 694 | naturalDeviceHeight = mViewport.deviceWidth; | 
|  | 695 | break; | 
|  | 696 | case DISPLAY_ORIENTATION_0: | 
|  | 697 | default: | 
|  | 698 | naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft; | 
|  | 699 | naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop; | 
|  | 700 | naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft; | 
|  | 701 | naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop; | 
|  | 702 | naturalPhysicalLeft = mViewport.physicalLeft; | 
|  | 703 | naturalPhysicalTop = mViewport.physicalTop; | 
|  | 704 | naturalDeviceWidth = mViewport.deviceWidth; | 
|  | 705 | naturalDeviceHeight = mViewport.deviceHeight; | 
|  | 706 | break; | 
|  | 707 | } | 
|  | 708 |  | 
|  | 709 | if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) { | 
|  | 710 | ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str()); | 
|  | 711 | naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight; | 
|  | 712 | naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth; | 
|  | 713 | } | 
|  | 714 |  | 
|  | 715 | mPhysicalWidth = naturalPhysicalWidth; | 
|  | 716 | mPhysicalHeight = naturalPhysicalHeight; | 
|  | 717 | mPhysicalLeft = naturalPhysicalLeft; | 
|  | 718 | mPhysicalTop = naturalPhysicalTop; | 
|  | 719 |  | 
|  | 720 | mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth; | 
|  | 721 | mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight; | 
|  | 722 | mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth; | 
|  | 723 | mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight; | 
|  | 724 |  | 
|  | 725 | mSurfaceOrientation = | 
|  | 726 | mParameters.orientationAware ? mViewport.orientation : DISPLAY_ORIENTATION_0; | 
|  | 727 | } else { | 
|  | 728 | mPhysicalWidth = rawWidth; | 
|  | 729 | mPhysicalHeight = rawHeight; | 
|  | 730 | mPhysicalLeft = 0; | 
|  | 731 | mPhysicalTop = 0; | 
|  | 732 |  | 
|  | 733 | mSurfaceWidth = rawWidth; | 
|  | 734 | mSurfaceHeight = rawHeight; | 
|  | 735 | mSurfaceLeft = 0; | 
|  | 736 | mSurfaceTop = 0; | 
|  | 737 | mSurfaceOrientation = DISPLAY_ORIENTATION_0; | 
|  | 738 | } | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 | // If moving between pointer modes, need to reset some state. | 
|  | 742 | bool deviceModeChanged = mDeviceMode != oldDeviceMode; | 
|  | 743 | if (deviceModeChanged) { | 
|  | 744 | mOrientedRanges.clear(); | 
|  | 745 | } | 
|  | 746 |  | 
|  | 747 | // Create or update pointer controller if needed. | 
|  | 748 | if (mDeviceMode == DEVICE_MODE_POINTER || | 
|  | 749 | (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) { | 
|  | 750 | if (mPointerController == nullptr || viewportChanged) { | 
|  | 751 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); | 
|  | 752 | } | 
|  | 753 | } else { | 
|  | 754 | mPointerController.clear(); | 
|  | 755 | } | 
|  | 756 |  | 
|  | 757 | if (viewportChanged || deviceModeChanged) { | 
|  | 758 | ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, " | 
|  | 759 | "display id %d", | 
|  | 760 | getDeviceId(), getDeviceName().c_str(), mSurfaceWidth, mSurfaceHeight, | 
|  | 761 | mSurfaceOrientation, mDeviceMode, mViewport.displayId); | 
|  | 762 |  | 
|  | 763 | // Configure X and Y factors. | 
|  | 764 | mXScale = float(mSurfaceWidth) / rawWidth; | 
|  | 765 | mYScale = float(mSurfaceHeight) / rawHeight; | 
|  | 766 | mXTranslate = -mSurfaceLeft; | 
|  | 767 | mYTranslate = -mSurfaceTop; | 
|  | 768 | mXPrecision = 1.0f / mXScale; | 
|  | 769 | mYPrecision = 1.0f / mYScale; | 
|  | 770 |  | 
|  | 771 | mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X; | 
|  | 772 | mOrientedRanges.x.source = mSource; | 
|  | 773 | mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; | 
|  | 774 | mOrientedRanges.y.source = mSource; | 
|  | 775 |  | 
|  | 776 | configureVirtualKeys(); | 
|  | 777 |  | 
|  | 778 | // Scale factor for terms that are not oriented in a particular axis. | 
|  | 779 | // If the pixels are square then xScale == yScale otherwise we fake it | 
|  | 780 | // by choosing an average. | 
|  | 781 | mGeometricScale = avg(mXScale, mYScale); | 
|  | 782 |  | 
|  | 783 | // Size of diagonal axis. | 
|  | 784 | float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight); | 
|  | 785 |  | 
|  | 786 | // Size factors. | 
|  | 787 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { | 
|  | 788 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) { | 
|  | 789 | mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue; | 
|  | 790 | } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) { | 
|  | 791 | mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue; | 
|  | 792 | } else { | 
|  | 793 | mSizeScale = 0.0f; | 
|  | 794 | } | 
|  | 795 |  | 
|  | 796 | mOrientedRanges.haveTouchSize = true; | 
|  | 797 | mOrientedRanges.haveToolSize = true; | 
|  | 798 | mOrientedRanges.haveSize = true; | 
|  | 799 |  | 
|  | 800 | mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; | 
|  | 801 | mOrientedRanges.touchMajor.source = mSource; | 
|  | 802 | mOrientedRanges.touchMajor.min = 0; | 
|  | 803 | mOrientedRanges.touchMajor.max = diagonalSize; | 
|  | 804 | mOrientedRanges.touchMajor.flat = 0; | 
|  | 805 | mOrientedRanges.touchMajor.fuzz = 0; | 
|  | 806 | mOrientedRanges.touchMajor.resolution = 0; | 
|  | 807 |  | 
|  | 808 | mOrientedRanges.touchMinor = mOrientedRanges.touchMajor; | 
|  | 809 | mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; | 
|  | 810 |  | 
|  | 811 | mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; | 
|  | 812 | mOrientedRanges.toolMajor.source = mSource; | 
|  | 813 | mOrientedRanges.toolMajor.min = 0; | 
|  | 814 | mOrientedRanges.toolMajor.max = diagonalSize; | 
|  | 815 | mOrientedRanges.toolMajor.flat = 0; | 
|  | 816 | mOrientedRanges.toolMajor.fuzz = 0; | 
|  | 817 | mOrientedRanges.toolMajor.resolution = 0; | 
|  | 818 |  | 
|  | 819 | mOrientedRanges.toolMinor = mOrientedRanges.toolMajor; | 
|  | 820 | mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; | 
|  | 821 |  | 
|  | 822 | mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; | 
|  | 823 | mOrientedRanges.size.source = mSource; | 
|  | 824 | mOrientedRanges.size.min = 0; | 
|  | 825 | mOrientedRanges.size.max = 1.0; | 
|  | 826 | mOrientedRanges.size.flat = 0; | 
|  | 827 | mOrientedRanges.size.fuzz = 0; | 
|  | 828 | mOrientedRanges.size.resolution = 0; | 
|  | 829 | } else { | 
|  | 830 | mSizeScale = 0.0f; | 
|  | 831 | } | 
|  | 832 |  | 
|  | 833 | // Pressure factors. | 
|  | 834 | mPressureScale = 0; | 
|  | 835 | float pressureMax = 1.0; | 
|  | 836 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL || | 
|  | 837 | mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { | 
|  | 838 | if (mCalibration.havePressureScale) { | 
|  | 839 | mPressureScale = mCalibration.pressureScale; | 
|  | 840 | pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue; | 
|  | 841 | } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) { | 
|  | 842 | mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue; | 
|  | 843 | } | 
|  | 844 | } | 
|  | 845 |  | 
|  | 846 | mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; | 
|  | 847 | mOrientedRanges.pressure.source = mSource; | 
|  | 848 | mOrientedRanges.pressure.min = 0; | 
|  | 849 | mOrientedRanges.pressure.max = pressureMax; | 
|  | 850 | mOrientedRanges.pressure.flat = 0; | 
|  | 851 | mOrientedRanges.pressure.fuzz = 0; | 
|  | 852 | mOrientedRanges.pressure.resolution = 0; | 
|  | 853 |  | 
|  | 854 | // Tilt | 
|  | 855 | mTiltXCenter = 0; | 
|  | 856 | mTiltXScale = 0; | 
|  | 857 | mTiltYCenter = 0; | 
|  | 858 | mTiltYScale = 0; | 
|  | 859 | mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid; | 
|  | 860 | if (mHaveTilt) { | 
|  | 861 | mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue); | 
|  | 862 | mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue); | 
|  | 863 | mTiltXScale = M_PI / 180; | 
|  | 864 | mTiltYScale = M_PI / 180; | 
|  | 865 |  | 
|  | 866 | mOrientedRanges.haveTilt = true; | 
|  | 867 |  | 
|  | 868 | mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT; | 
|  | 869 | mOrientedRanges.tilt.source = mSource; | 
|  | 870 | mOrientedRanges.tilt.min = 0; | 
|  | 871 | mOrientedRanges.tilt.max = M_PI_2; | 
|  | 872 | mOrientedRanges.tilt.flat = 0; | 
|  | 873 | mOrientedRanges.tilt.fuzz = 0; | 
|  | 874 | mOrientedRanges.tilt.resolution = 0; | 
|  | 875 | } | 
|  | 876 |  | 
|  | 877 | // Orientation | 
|  | 878 | mOrientationScale = 0; | 
|  | 879 | if (mHaveTilt) { | 
|  | 880 | mOrientedRanges.haveOrientation = true; | 
|  | 881 |  | 
|  | 882 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; | 
|  | 883 | mOrientedRanges.orientation.source = mSource; | 
|  | 884 | mOrientedRanges.orientation.min = -M_PI; | 
|  | 885 | mOrientedRanges.orientation.max = M_PI; | 
|  | 886 | mOrientedRanges.orientation.flat = 0; | 
|  | 887 | mOrientedRanges.orientation.fuzz = 0; | 
|  | 888 | mOrientedRanges.orientation.resolution = 0; | 
|  | 889 | } else if (mCalibration.orientationCalibration != | 
|  | 890 | Calibration::ORIENTATION_CALIBRATION_NONE) { | 
|  | 891 | if (mCalibration.orientationCalibration == | 
|  | 892 | Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { | 
|  | 893 | if (mRawPointerAxes.orientation.valid) { | 
|  | 894 | if (mRawPointerAxes.orientation.maxValue > 0) { | 
|  | 895 | mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue; | 
|  | 896 | } else if (mRawPointerAxes.orientation.minValue < 0) { | 
|  | 897 | mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue; | 
|  | 898 | } else { | 
|  | 899 | mOrientationScale = 0; | 
|  | 900 | } | 
|  | 901 | } | 
|  | 902 | } | 
|  | 903 |  | 
|  | 904 | mOrientedRanges.haveOrientation = true; | 
|  | 905 |  | 
|  | 906 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; | 
|  | 907 | mOrientedRanges.orientation.source = mSource; | 
|  | 908 | mOrientedRanges.orientation.min = -M_PI_2; | 
|  | 909 | mOrientedRanges.orientation.max = M_PI_2; | 
|  | 910 | mOrientedRanges.orientation.flat = 0; | 
|  | 911 | mOrientedRanges.orientation.fuzz = 0; | 
|  | 912 | mOrientedRanges.orientation.resolution = 0; | 
|  | 913 | } | 
|  | 914 |  | 
|  | 915 | // Distance | 
|  | 916 | mDistanceScale = 0; | 
|  | 917 | if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) { | 
|  | 918 | if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_SCALED) { | 
|  | 919 | if (mCalibration.haveDistanceScale) { | 
|  | 920 | mDistanceScale = mCalibration.distanceScale; | 
|  | 921 | } else { | 
|  | 922 | mDistanceScale = 1.0f; | 
|  | 923 | } | 
|  | 924 | } | 
|  | 925 |  | 
|  | 926 | mOrientedRanges.haveDistance = true; | 
|  | 927 |  | 
|  | 928 | mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE; | 
|  | 929 | mOrientedRanges.distance.source = mSource; | 
|  | 930 | mOrientedRanges.distance.min = mRawPointerAxes.distance.minValue * mDistanceScale; | 
|  | 931 | mOrientedRanges.distance.max = mRawPointerAxes.distance.maxValue * mDistanceScale; | 
|  | 932 | mOrientedRanges.distance.flat = 0; | 
|  | 933 | mOrientedRanges.distance.fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale; | 
|  | 934 | mOrientedRanges.distance.resolution = 0; | 
|  | 935 | } | 
|  | 936 |  | 
|  | 937 | // Compute oriented precision, scales and ranges. | 
|  | 938 | // Note that the maximum value reported is an inclusive maximum value so it is one | 
|  | 939 | // unit less than the total width or height of surface. | 
|  | 940 | switch (mSurfaceOrientation) { | 
|  | 941 | case DISPLAY_ORIENTATION_90: | 
|  | 942 | case DISPLAY_ORIENTATION_270: | 
|  | 943 | mOrientedXPrecision = mYPrecision; | 
|  | 944 | mOrientedYPrecision = mXPrecision; | 
|  | 945 |  | 
|  | 946 | mOrientedRanges.x.min = mYTranslate; | 
|  | 947 | mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1; | 
|  | 948 | mOrientedRanges.x.flat = 0; | 
|  | 949 | mOrientedRanges.x.fuzz = 0; | 
|  | 950 | mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale; | 
|  | 951 |  | 
|  | 952 | mOrientedRanges.y.min = mXTranslate; | 
|  | 953 | mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1; | 
|  | 954 | mOrientedRanges.y.flat = 0; | 
|  | 955 | mOrientedRanges.y.fuzz = 0; | 
|  | 956 | mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale; | 
|  | 957 | break; | 
|  | 958 |  | 
|  | 959 | default: | 
|  | 960 | mOrientedXPrecision = mXPrecision; | 
|  | 961 | mOrientedYPrecision = mYPrecision; | 
|  | 962 |  | 
|  | 963 | mOrientedRanges.x.min = mXTranslate; | 
|  | 964 | mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1; | 
|  | 965 | mOrientedRanges.x.flat = 0; | 
|  | 966 | mOrientedRanges.x.fuzz = 0; | 
|  | 967 | mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale; | 
|  | 968 |  | 
|  | 969 | mOrientedRanges.y.min = mYTranslate; | 
|  | 970 | mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1; | 
|  | 971 | mOrientedRanges.y.flat = 0; | 
|  | 972 | mOrientedRanges.y.fuzz = 0; | 
|  | 973 | mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale; | 
|  | 974 | break; | 
|  | 975 | } | 
|  | 976 |  | 
|  | 977 | // Location | 
|  | 978 | updateAffineTransformation(); | 
|  | 979 |  | 
|  | 980 | if (mDeviceMode == DEVICE_MODE_POINTER) { | 
|  | 981 | // Compute pointer gesture detection parameters. | 
|  | 982 | float rawDiagonal = hypotf(rawWidth, rawHeight); | 
|  | 983 | float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight); | 
|  | 984 |  | 
|  | 985 | // Scale movements such that one whole swipe of the touch pad covers a | 
|  | 986 | // given area relative to the diagonal size of the display when no acceleration | 
|  | 987 | // is applied. | 
|  | 988 | // Assume that the touch pad has a square aspect ratio such that movements in | 
|  | 989 | // X and Y of the same number of raw units cover the same physical distance. | 
|  | 990 | mPointerXMovementScale = | 
|  | 991 | mConfig.pointerGestureMovementSpeedRatio * displayDiagonal / rawDiagonal; | 
|  | 992 | mPointerYMovementScale = mPointerXMovementScale; | 
|  | 993 |  | 
|  | 994 | // Scale zooms to cover a smaller range of the display than movements do. | 
|  | 995 | // This value determines the area around the pointer that is affected by freeform | 
|  | 996 | // pointer gestures. | 
|  | 997 | mPointerXZoomScale = | 
|  | 998 | mConfig.pointerGestureZoomSpeedRatio * displayDiagonal / rawDiagonal; | 
|  | 999 | mPointerYZoomScale = mPointerXZoomScale; | 
|  | 1000 |  | 
|  | 1001 | // Max width between pointers to detect a swipe gesture is more than some fraction | 
|  | 1002 | // of the diagonal axis of the touch pad.  Touches that are wider than this are | 
|  | 1003 | // translated into freeform gestures. | 
|  | 1004 | mPointerGestureMaxSwipeWidth = mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal; | 
|  | 1005 |  | 
|  | 1006 | // Abort current pointer usages because the state has changed. | 
|  | 1007 | abortPointerUsage(when, 0 /*policyFlags*/); | 
|  | 1008 | } | 
|  | 1009 |  | 
|  | 1010 | // Inform the dispatcher about the changes. | 
|  | 1011 | *outResetNeeded = true; | 
|  | 1012 | bumpGeneration(); | 
|  | 1013 | } | 
|  | 1014 | } | 
|  | 1015 |  | 
|  | 1016 | void TouchInputMapper::dumpSurface(std::string& dump) { | 
|  | 1017 | dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str()); | 
|  | 1018 | dump += StringPrintf(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth); | 
|  | 1019 | dump += StringPrintf(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight); | 
|  | 1020 | dump += StringPrintf(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft); | 
|  | 1021 | dump += StringPrintf(INDENT3 "SurfaceTop: %d\n", mSurfaceTop); | 
|  | 1022 | dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth); | 
|  | 1023 | dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight); | 
|  | 1024 | dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft); | 
|  | 1025 | dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop); | 
|  | 1026 | dump += StringPrintf(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation); | 
|  | 1027 | } | 
|  | 1028 |  | 
|  | 1029 | void TouchInputMapper::configureVirtualKeys() { | 
|  | 1030 | std::vector<VirtualKeyDefinition> virtualKeyDefinitions; | 
|  | 1031 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); | 
|  | 1032 |  | 
|  | 1033 | mVirtualKeys.clear(); | 
|  | 1034 |  | 
|  | 1035 | if (virtualKeyDefinitions.size() == 0) { | 
|  | 1036 | return; | 
|  | 1037 | } | 
|  | 1038 |  | 
|  | 1039 | int32_t touchScreenLeft = mRawPointerAxes.x.minValue; | 
|  | 1040 | int32_t touchScreenTop = mRawPointerAxes.y.minValue; | 
|  | 1041 | int32_t touchScreenWidth = mRawPointerAxes.getRawWidth(); | 
|  | 1042 | int32_t touchScreenHeight = mRawPointerAxes.getRawHeight(); | 
|  | 1043 |  | 
|  | 1044 | for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) { | 
|  | 1045 | VirtualKey virtualKey; | 
|  | 1046 |  | 
|  | 1047 | virtualKey.scanCode = virtualKeyDefinition.scanCode; | 
|  | 1048 | int32_t keyCode; | 
|  | 1049 | int32_t dummyKeyMetaState; | 
|  | 1050 | uint32_t flags; | 
|  | 1051 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, 0, &keyCode, | 
|  | 1052 | &dummyKeyMetaState, &flags)) { | 
|  | 1053 | ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode); | 
|  | 1054 | continue; // drop the key | 
|  | 1055 | } | 
|  | 1056 |  | 
|  | 1057 | virtualKey.keyCode = keyCode; | 
|  | 1058 | virtualKey.flags = flags; | 
|  | 1059 |  | 
|  | 1060 | // convert the key definition's display coordinates into touch coordinates for a hit box | 
|  | 1061 | int32_t halfWidth = virtualKeyDefinition.width / 2; | 
|  | 1062 | int32_t halfHeight = virtualKeyDefinition.height / 2; | 
|  | 1063 |  | 
|  | 1064 | virtualKey.hitLeft = | 
|  | 1065 | (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mSurfaceWidth + | 
|  | 1066 | touchScreenLeft; | 
|  | 1067 | virtualKey.hitRight = | 
|  | 1068 | (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mSurfaceWidth + | 
|  | 1069 | touchScreenLeft; | 
|  | 1070 | virtualKey.hitTop = | 
|  | 1071 | (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mSurfaceHeight + | 
|  | 1072 | touchScreenTop; | 
|  | 1073 | virtualKey.hitBottom = | 
|  | 1074 | (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mSurfaceHeight + | 
|  | 1075 | touchScreenTop; | 
|  | 1076 | mVirtualKeys.push_back(virtualKey); | 
|  | 1077 | } | 
|  | 1078 | } | 
|  | 1079 |  | 
|  | 1080 | void TouchInputMapper::dumpVirtualKeys(std::string& dump) { | 
|  | 1081 | if (!mVirtualKeys.empty()) { | 
|  | 1082 | dump += INDENT3 "Virtual Keys:\n"; | 
|  | 1083 |  | 
|  | 1084 | for (size_t i = 0; i < mVirtualKeys.size(); i++) { | 
|  | 1085 | const VirtualKey& virtualKey = mVirtualKeys[i]; | 
|  | 1086 | dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, " | 
|  | 1087 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", | 
|  | 1088 | i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft, | 
|  | 1089 | virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom); | 
|  | 1090 | } | 
|  | 1091 | } | 
|  | 1092 | } | 
|  | 1093 |  | 
|  | 1094 | void TouchInputMapper::parseCalibration() { | 
|  | 1095 | const PropertyMap& in = getDevice()->getConfiguration(); | 
|  | 1096 | Calibration& out = mCalibration; | 
|  | 1097 |  | 
|  | 1098 | // Size | 
|  | 1099 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; | 
|  | 1100 | String8 sizeCalibrationString; | 
|  | 1101 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { | 
|  | 1102 | if (sizeCalibrationString == "none") { | 
|  | 1103 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; | 
|  | 1104 | } else if (sizeCalibrationString == "geometric") { | 
|  | 1105 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; | 
|  | 1106 | } else if (sizeCalibrationString == "diameter") { | 
|  | 1107 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER; | 
|  | 1108 | } else if (sizeCalibrationString == "box") { | 
|  | 1109 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX; | 
|  | 1110 | } else if (sizeCalibrationString == "area") { | 
|  | 1111 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA; | 
|  | 1112 | } else if (sizeCalibrationString != "default") { | 
|  | 1113 | ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.string()); | 
|  | 1114 | } | 
|  | 1115 | } | 
|  | 1116 |  | 
|  | 1117 | out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), out.sizeScale); | 
|  | 1118 | out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), out.sizeBias); | 
|  | 1119 | out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), out.sizeIsSummed); | 
|  | 1120 |  | 
|  | 1121 | // Pressure | 
|  | 1122 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; | 
|  | 1123 | String8 pressureCalibrationString; | 
|  | 1124 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { | 
|  | 1125 | if (pressureCalibrationString == "none") { | 
|  | 1126 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; | 
|  | 1127 | } else if (pressureCalibrationString == "physical") { | 
|  | 1128 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; | 
|  | 1129 | } else if (pressureCalibrationString == "amplitude") { | 
|  | 1130 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; | 
|  | 1131 | } else if (pressureCalibrationString != "default") { | 
|  | 1132 | ALOGW("Invalid value for touch.pressure.calibration: '%s'", | 
|  | 1133 | pressureCalibrationString.string()); | 
|  | 1134 | } | 
|  | 1135 | } | 
|  | 1136 |  | 
|  | 1137 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), out.pressureScale); | 
|  | 1138 |  | 
|  | 1139 | // Orientation | 
|  | 1140 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; | 
|  | 1141 | String8 orientationCalibrationString; | 
|  | 1142 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { | 
|  | 1143 | if (orientationCalibrationString == "none") { | 
|  | 1144 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; | 
|  | 1145 | } else if (orientationCalibrationString == "interpolated") { | 
|  | 1146 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; | 
|  | 1147 | } else if (orientationCalibrationString == "vector") { | 
|  | 1148 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; | 
|  | 1149 | } else if (orientationCalibrationString != "default") { | 
|  | 1150 | ALOGW("Invalid value for touch.orientation.calibration: '%s'", | 
|  | 1151 | orientationCalibrationString.string()); | 
|  | 1152 | } | 
|  | 1153 | } | 
|  | 1154 |  | 
|  | 1155 | // Distance | 
|  | 1156 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT; | 
|  | 1157 | String8 distanceCalibrationString; | 
|  | 1158 | if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) { | 
|  | 1159 | if (distanceCalibrationString == "none") { | 
|  | 1160 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; | 
|  | 1161 | } else if (distanceCalibrationString == "scaled") { | 
|  | 1162 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; | 
|  | 1163 | } else if (distanceCalibrationString != "default") { | 
|  | 1164 | ALOGW("Invalid value for touch.distance.calibration: '%s'", | 
|  | 1165 | distanceCalibrationString.string()); | 
|  | 1166 | } | 
|  | 1167 | } | 
|  | 1168 |  | 
|  | 1169 | out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), out.distanceScale); | 
|  | 1170 |  | 
|  | 1171 | out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_DEFAULT; | 
|  | 1172 | String8 coverageCalibrationString; | 
|  | 1173 | if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) { | 
|  | 1174 | if (coverageCalibrationString == "none") { | 
|  | 1175 | out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE; | 
|  | 1176 | } else if (coverageCalibrationString == "box") { | 
|  | 1177 | out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_BOX; | 
|  | 1178 | } else if (coverageCalibrationString != "default") { | 
|  | 1179 | ALOGW("Invalid value for touch.coverage.calibration: '%s'", | 
|  | 1180 | coverageCalibrationString.string()); | 
|  | 1181 | } | 
|  | 1182 | } | 
|  | 1183 | } | 
|  | 1184 |  | 
|  | 1185 | void TouchInputMapper::resolveCalibration() { | 
|  | 1186 | // Size | 
|  | 1187 | if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) { | 
|  | 1188 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) { | 
|  | 1189 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; | 
|  | 1190 | } | 
|  | 1191 | } else { | 
|  | 1192 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; | 
|  | 1193 | } | 
|  | 1194 |  | 
|  | 1195 | // Pressure | 
|  | 1196 | if (mRawPointerAxes.pressure.valid) { | 
|  | 1197 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) { | 
|  | 1198 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; | 
|  | 1199 | } | 
|  | 1200 | } else { | 
|  | 1201 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; | 
|  | 1202 | } | 
|  | 1203 |  | 
|  | 1204 | // Orientation | 
|  | 1205 | if (mRawPointerAxes.orientation.valid) { | 
|  | 1206 | if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) { | 
|  | 1207 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; | 
|  | 1208 | } | 
|  | 1209 | } else { | 
|  | 1210 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; | 
|  | 1211 | } | 
|  | 1212 |  | 
|  | 1213 | // Distance | 
|  | 1214 | if (mRawPointerAxes.distance.valid) { | 
|  | 1215 | if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) { | 
|  | 1216 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; | 
|  | 1217 | } | 
|  | 1218 | } else { | 
|  | 1219 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; | 
|  | 1220 | } | 
|  | 1221 |  | 
|  | 1222 | // Coverage | 
|  | 1223 | if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_DEFAULT) { | 
|  | 1224 | mCalibration.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE; | 
|  | 1225 | } | 
|  | 1226 | } | 
|  | 1227 |  | 
|  | 1228 | void TouchInputMapper::dumpCalibration(std::string& dump) { | 
|  | 1229 | dump += INDENT3 "Calibration:\n"; | 
|  | 1230 |  | 
|  | 1231 | // Size | 
|  | 1232 | switch (mCalibration.sizeCalibration) { | 
|  | 1233 | case Calibration::SIZE_CALIBRATION_NONE: | 
|  | 1234 | dump += INDENT4 "touch.size.calibration: none\n"; | 
|  | 1235 | break; | 
|  | 1236 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: | 
|  | 1237 | dump += INDENT4 "touch.size.calibration: geometric\n"; | 
|  | 1238 | break; | 
|  | 1239 | case Calibration::SIZE_CALIBRATION_DIAMETER: | 
|  | 1240 | dump += INDENT4 "touch.size.calibration: diameter\n"; | 
|  | 1241 | break; | 
|  | 1242 | case Calibration::SIZE_CALIBRATION_BOX: | 
|  | 1243 | dump += INDENT4 "touch.size.calibration: box\n"; | 
|  | 1244 | break; | 
|  | 1245 | case Calibration::SIZE_CALIBRATION_AREA: | 
|  | 1246 | dump += INDENT4 "touch.size.calibration: area\n"; | 
|  | 1247 | break; | 
|  | 1248 | default: | 
|  | 1249 | ALOG_ASSERT(false); | 
|  | 1250 | } | 
|  | 1251 |  | 
|  | 1252 | if (mCalibration.haveSizeScale) { | 
|  | 1253 | dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", mCalibration.sizeScale); | 
|  | 1254 | } | 
|  | 1255 |  | 
|  | 1256 | if (mCalibration.haveSizeBias) { | 
|  | 1257 | dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", mCalibration.sizeBias); | 
|  | 1258 | } | 
|  | 1259 |  | 
|  | 1260 | if (mCalibration.haveSizeIsSummed) { | 
|  | 1261 | dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n", | 
|  | 1262 | toString(mCalibration.sizeIsSummed)); | 
|  | 1263 | } | 
|  | 1264 |  | 
|  | 1265 | // Pressure | 
|  | 1266 | switch (mCalibration.pressureCalibration) { | 
|  | 1267 | case Calibration::PRESSURE_CALIBRATION_NONE: | 
|  | 1268 | dump += INDENT4 "touch.pressure.calibration: none\n"; | 
|  | 1269 | break; | 
|  | 1270 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: | 
|  | 1271 | dump += INDENT4 "touch.pressure.calibration: physical\n"; | 
|  | 1272 | break; | 
|  | 1273 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: | 
|  | 1274 | dump += INDENT4 "touch.pressure.calibration: amplitude\n"; | 
|  | 1275 | break; | 
|  | 1276 | default: | 
|  | 1277 | ALOG_ASSERT(false); | 
|  | 1278 | } | 
|  | 1279 |  | 
|  | 1280 | if (mCalibration.havePressureScale) { | 
|  | 1281 | dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", mCalibration.pressureScale); | 
|  | 1282 | } | 
|  | 1283 |  | 
|  | 1284 | // Orientation | 
|  | 1285 | switch (mCalibration.orientationCalibration) { | 
|  | 1286 | case Calibration::ORIENTATION_CALIBRATION_NONE: | 
|  | 1287 | dump += INDENT4 "touch.orientation.calibration: none\n"; | 
|  | 1288 | break; | 
|  | 1289 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: | 
|  | 1290 | dump += INDENT4 "touch.orientation.calibration: interpolated\n"; | 
|  | 1291 | break; | 
|  | 1292 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: | 
|  | 1293 | dump += INDENT4 "touch.orientation.calibration: vector\n"; | 
|  | 1294 | break; | 
|  | 1295 | default: | 
|  | 1296 | ALOG_ASSERT(false); | 
|  | 1297 | } | 
|  | 1298 |  | 
|  | 1299 | // Distance | 
|  | 1300 | switch (mCalibration.distanceCalibration) { | 
|  | 1301 | case Calibration::DISTANCE_CALIBRATION_NONE: | 
|  | 1302 | dump += INDENT4 "touch.distance.calibration: none\n"; | 
|  | 1303 | break; | 
|  | 1304 | case Calibration::DISTANCE_CALIBRATION_SCALED: | 
|  | 1305 | dump += INDENT4 "touch.distance.calibration: scaled\n"; | 
|  | 1306 | break; | 
|  | 1307 | default: | 
|  | 1308 | ALOG_ASSERT(false); | 
|  | 1309 | } | 
|  | 1310 |  | 
|  | 1311 | if (mCalibration.haveDistanceScale) { | 
|  | 1312 | dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", mCalibration.distanceScale); | 
|  | 1313 | } | 
|  | 1314 |  | 
|  | 1315 | switch (mCalibration.coverageCalibration) { | 
|  | 1316 | case Calibration::COVERAGE_CALIBRATION_NONE: | 
|  | 1317 | dump += INDENT4 "touch.coverage.calibration: none\n"; | 
|  | 1318 | break; | 
|  | 1319 | case Calibration::COVERAGE_CALIBRATION_BOX: | 
|  | 1320 | dump += INDENT4 "touch.coverage.calibration: box\n"; | 
|  | 1321 | break; | 
|  | 1322 | default: | 
|  | 1323 | ALOG_ASSERT(false); | 
|  | 1324 | } | 
|  | 1325 | } | 
|  | 1326 |  | 
|  | 1327 | void TouchInputMapper::dumpAffineTransformation(std::string& dump) { | 
|  | 1328 | dump += INDENT3 "Affine Transformation:\n"; | 
|  | 1329 |  | 
|  | 1330 | dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale); | 
|  | 1331 | dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix); | 
|  | 1332 | dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset); | 
|  | 1333 | dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix); | 
|  | 1334 | dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale); | 
|  | 1335 | dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset); | 
|  | 1336 | } | 
|  | 1337 |  | 
|  | 1338 | void TouchInputMapper::updateAffineTransformation() { | 
|  | 1339 | mAffineTransform = getPolicy()->getTouchAffineTransformation(mDevice->getDescriptor(), | 
|  | 1340 | mSurfaceOrientation); | 
|  | 1341 | } | 
|  | 1342 |  | 
|  | 1343 | void TouchInputMapper::reset(nsecs_t when) { | 
|  | 1344 | mCursorButtonAccumulator.reset(getDevice()); | 
|  | 1345 | mCursorScrollAccumulator.reset(getDevice()); | 
|  | 1346 | mTouchButtonAccumulator.reset(getDevice()); | 
|  | 1347 |  | 
|  | 1348 | mPointerVelocityControl.reset(); | 
|  | 1349 | mWheelXVelocityControl.reset(); | 
|  | 1350 | mWheelYVelocityControl.reset(); | 
|  | 1351 |  | 
|  | 1352 | mRawStatesPending.clear(); | 
|  | 1353 | mCurrentRawState.clear(); | 
|  | 1354 | mCurrentCookedState.clear(); | 
|  | 1355 | mLastRawState.clear(); | 
|  | 1356 | mLastCookedState.clear(); | 
|  | 1357 | mPointerUsage = POINTER_USAGE_NONE; | 
|  | 1358 | mSentHoverEnter = false; | 
|  | 1359 | mHavePointerIds = false; | 
|  | 1360 | mCurrentMotionAborted = false; | 
|  | 1361 | mDownTime = 0; | 
|  | 1362 |  | 
|  | 1363 | mCurrentVirtualKey.down = false; | 
|  | 1364 |  | 
|  | 1365 | mPointerGesture.reset(); | 
|  | 1366 | mPointerSimple.reset(); | 
|  | 1367 | resetExternalStylus(); | 
|  | 1368 |  | 
|  | 1369 | if (mPointerController != nullptr) { | 
|  | 1370 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); | 
|  | 1371 | mPointerController->clearSpots(); | 
|  | 1372 | } | 
|  | 1373 |  | 
|  | 1374 | InputMapper::reset(when); | 
|  | 1375 | } | 
|  | 1376 |  | 
|  | 1377 | void TouchInputMapper::resetExternalStylus() { | 
|  | 1378 | mExternalStylusState.clear(); | 
|  | 1379 | mExternalStylusId = -1; | 
|  | 1380 | mExternalStylusFusionTimeout = LLONG_MAX; | 
|  | 1381 | mExternalStylusDataPending = false; | 
|  | 1382 | } | 
|  | 1383 |  | 
|  | 1384 | void TouchInputMapper::clearStylusDataPendingFlags() { | 
|  | 1385 | mExternalStylusDataPending = false; | 
|  | 1386 | mExternalStylusFusionTimeout = LLONG_MAX; | 
|  | 1387 | } | 
|  | 1388 |  | 
|  | 1389 | void TouchInputMapper::process(const RawEvent* rawEvent) { | 
|  | 1390 | mCursorButtonAccumulator.process(rawEvent); | 
|  | 1391 | mCursorScrollAccumulator.process(rawEvent); | 
|  | 1392 | mTouchButtonAccumulator.process(rawEvent); | 
|  | 1393 |  | 
|  | 1394 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { | 
|  | 1395 | sync(rawEvent->when); | 
|  | 1396 | } | 
|  | 1397 | } | 
|  | 1398 |  | 
|  | 1399 | void TouchInputMapper::sync(nsecs_t when) { | 
|  | 1400 | const RawState* last = | 
|  | 1401 | mRawStatesPending.empty() ? &mCurrentRawState : &mRawStatesPending.back(); | 
|  | 1402 |  | 
|  | 1403 | // Push a new state. | 
|  | 1404 | mRawStatesPending.emplace_back(); | 
|  | 1405 |  | 
|  | 1406 | RawState* next = &mRawStatesPending.back(); | 
|  | 1407 | next->clear(); | 
|  | 1408 | next->when = when; | 
|  | 1409 |  | 
|  | 1410 | // Sync button state. | 
|  | 1411 | next->buttonState = | 
|  | 1412 | mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState(); | 
|  | 1413 |  | 
|  | 1414 | // Sync scroll | 
|  | 1415 | next->rawVScroll = mCursorScrollAccumulator.getRelativeVWheel(); | 
|  | 1416 | next->rawHScroll = mCursorScrollAccumulator.getRelativeHWheel(); | 
|  | 1417 | mCursorScrollAccumulator.finishSync(); | 
|  | 1418 |  | 
|  | 1419 | // Sync touch | 
|  | 1420 | syncTouch(when, next); | 
|  | 1421 |  | 
|  | 1422 | // Assign pointer ids. | 
|  | 1423 | if (!mHavePointerIds) { | 
|  | 1424 | assignPointerIds(last, next); | 
|  | 1425 | } | 
|  | 1426 |  | 
|  | 1427 | #if DEBUG_RAW_EVENTS | 
|  | 1428 | ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, " | 
|  | 1429 | "hovering ids 0x%08x -> 0x%08x", | 
|  | 1430 | last->rawPointerData.pointerCount, next->rawPointerData.pointerCount, | 
|  | 1431 | last->rawPointerData.touchingIdBits.value, next->rawPointerData.touchingIdBits.value, | 
|  | 1432 | last->rawPointerData.hoveringIdBits.value, next->rawPointerData.hoveringIdBits.value); | 
|  | 1433 | #endif | 
|  | 1434 |  | 
|  | 1435 | processRawTouches(false /*timeout*/); | 
|  | 1436 | } | 
|  | 1437 |  | 
|  | 1438 | void TouchInputMapper::processRawTouches(bool timeout) { | 
|  | 1439 | if (mDeviceMode == DEVICE_MODE_DISABLED) { | 
|  | 1440 | // Drop all input if the device is disabled. | 
|  | 1441 | mCurrentRawState.clear(); | 
|  | 1442 | mRawStatesPending.clear(); | 
|  | 1443 | return; | 
|  | 1444 | } | 
|  | 1445 |  | 
|  | 1446 | // Drain any pending touch states. The invariant here is that the mCurrentRawState is always | 
|  | 1447 | // valid and must go through the full cook and dispatch cycle. This ensures that anything | 
|  | 1448 | // touching the current state will only observe the events that have been dispatched to the | 
|  | 1449 | // rest of the pipeline. | 
|  | 1450 | const size_t N = mRawStatesPending.size(); | 
|  | 1451 | size_t count; | 
|  | 1452 | for (count = 0; count < N; count++) { | 
|  | 1453 | const RawState& next = mRawStatesPending[count]; | 
|  | 1454 |  | 
|  | 1455 | // A failure to assign the stylus id means that we're waiting on stylus data | 
|  | 1456 | // and so should defer the rest of the pipeline. | 
|  | 1457 | if (assignExternalStylusId(next, timeout)) { | 
|  | 1458 | break; | 
|  | 1459 | } | 
|  | 1460 |  | 
|  | 1461 | // All ready to go. | 
|  | 1462 | clearStylusDataPendingFlags(); | 
|  | 1463 | mCurrentRawState.copyFrom(next); | 
|  | 1464 | if (mCurrentRawState.when < mLastRawState.when) { | 
|  | 1465 | mCurrentRawState.when = mLastRawState.when; | 
|  | 1466 | } | 
|  | 1467 | cookAndDispatch(mCurrentRawState.when); | 
|  | 1468 | } | 
|  | 1469 | if (count != 0) { | 
|  | 1470 | mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count); | 
|  | 1471 | } | 
|  | 1472 |  | 
|  | 1473 | if (mExternalStylusDataPending) { | 
|  | 1474 | if (timeout) { | 
|  | 1475 | nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY; | 
|  | 1476 | clearStylusDataPendingFlags(); | 
|  | 1477 | mCurrentRawState.copyFrom(mLastRawState); | 
|  | 1478 | #if DEBUG_STYLUS_FUSION | 
|  | 1479 | ALOGD("Timeout expired, synthesizing event with new stylus data"); | 
|  | 1480 | #endif | 
|  | 1481 | cookAndDispatch(when); | 
|  | 1482 | } else if (mExternalStylusFusionTimeout == LLONG_MAX) { | 
|  | 1483 | mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT; | 
|  | 1484 | getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout); | 
|  | 1485 | } | 
|  | 1486 | } | 
|  | 1487 | } | 
|  | 1488 |  | 
|  | 1489 | void TouchInputMapper::cookAndDispatch(nsecs_t when) { | 
|  | 1490 | // Always start with a clean state. | 
|  | 1491 | mCurrentCookedState.clear(); | 
|  | 1492 |  | 
|  | 1493 | // Apply stylus buttons to current raw state. | 
|  | 1494 | applyExternalStylusButtonState(when); | 
|  | 1495 |  | 
|  | 1496 | // Handle policy on initial down or hover events. | 
|  | 1497 | bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 && | 
|  | 1498 | mCurrentRawState.rawPointerData.pointerCount != 0; | 
|  | 1499 |  | 
|  | 1500 | uint32_t policyFlags = 0; | 
|  | 1501 | bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState; | 
|  | 1502 | if (initialDown || buttonsPressed) { | 
|  | 1503 | // If this is a touch screen, hide the pointer on an initial down. | 
|  | 1504 | if (mDeviceMode == DEVICE_MODE_DIRECT) { | 
|  | 1505 | getContext()->fadePointer(); | 
|  | 1506 | } | 
|  | 1507 |  | 
|  | 1508 | if (mParameters.wake) { | 
|  | 1509 | policyFlags |= POLICY_FLAG_WAKE; | 
|  | 1510 | } | 
|  | 1511 | } | 
|  | 1512 |  | 
|  | 1513 | // Consume raw off-screen touches before cooking pointer data. | 
|  | 1514 | // If touches are consumed, subsequent code will not receive any pointer data. | 
|  | 1515 | if (consumeRawTouches(when, policyFlags)) { | 
|  | 1516 | mCurrentRawState.rawPointerData.clear(); | 
|  | 1517 | } | 
|  | 1518 |  | 
|  | 1519 | // Cook pointer data.  This call populates the mCurrentCookedState.cookedPointerData structure | 
|  | 1520 | // with cooked pointer data that has the same ids and indices as the raw data. | 
|  | 1521 | // The following code can use either the raw or cooked data, as needed. | 
|  | 1522 | cookPointerData(); | 
|  | 1523 |  | 
|  | 1524 | // Apply stylus pressure to current cooked state. | 
|  | 1525 | applyExternalStylusTouchState(when); | 
|  | 1526 |  | 
|  | 1527 | // Synthesize key down from raw buttons if needed. | 
|  | 1528 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, | 
|  | 1529 | mViewport.displayId, policyFlags, mLastCookedState.buttonState, | 
|  | 1530 | mCurrentCookedState.buttonState); | 
|  | 1531 |  | 
|  | 1532 | // Dispatch the touches either directly or by translation through a pointer on screen. | 
|  | 1533 | if (mDeviceMode == DEVICE_MODE_POINTER) { | 
|  | 1534 | for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) { | 
|  | 1535 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 1536 | const RawPointerData::Pointer& pointer = | 
|  | 1537 | mCurrentRawState.rawPointerData.pointerForId(id); | 
|  | 1538 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || | 
|  | 1539 | pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { | 
|  | 1540 | mCurrentCookedState.stylusIdBits.markBit(id); | 
|  | 1541 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER || | 
|  | 1542 | pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { | 
|  | 1543 | mCurrentCookedState.fingerIdBits.markBit(id); | 
|  | 1544 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) { | 
|  | 1545 | mCurrentCookedState.mouseIdBits.markBit(id); | 
|  | 1546 | } | 
|  | 1547 | } | 
|  | 1548 | for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) { | 
|  | 1549 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 1550 | const RawPointerData::Pointer& pointer = | 
|  | 1551 | mCurrentRawState.rawPointerData.pointerForId(id); | 
|  | 1552 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || | 
|  | 1553 | pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { | 
|  | 1554 | mCurrentCookedState.stylusIdBits.markBit(id); | 
|  | 1555 | } | 
|  | 1556 | } | 
|  | 1557 |  | 
|  | 1558 | // Stylus takes precedence over all tools, then mouse, then finger. | 
|  | 1559 | PointerUsage pointerUsage = mPointerUsage; | 
|  | 1560 | if (!mCurrentCookedState.stylusIdBits.isEmpty()) { | 
|  | 1561 | mCurrentCookedState.mouseIdBits.clear(); | 
|  | 1562 | mCurrentCookedState.fingerIdBits.clear(); | 
|  | 1563 | pointerUsage = POINTER_USAGE_STYLUS; | 
|  | 1564 | } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) { | 
|  | 1565 | mCurrentCookedState.fingerIdBits.clear(); | 
|  | 1566 | pointerUsage = POINTER_USAGE_MOUSE; | 
|  | 1567 | } else if (!mCurrentCookedState.fingerIdBits.isEmpty() || | 
|  | 1568 | isPointerDown(mCurrentRawState.buttonState)) { | 
|  | 1569 | pointerUsage = POINTER_USAGE_GESTURES; | 
|  | 1570 | } | 
|  | 1571 |  | 
|  | 1572 | dispatchPointerUsage(when, policyFlags, pointerUsage); | 
|  | 1573 | } else { | 
|  | 1574 | if (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches && | 
|  | 1575 | mPointerController != nullptr) { | 
|  | 1576 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); | 
|  | 1577 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); | 
|  | 1578 |  | 
|  | 1579 | mPointerController->setButtonState(mCurrentRawState.buttonState); | 
|  | 1580 | mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords, | 
|  | 1581 | mCurrentCookedState.cookedPointerData.idToIndex, | 
|  | 1582 | mCurrentCookedState.cookedPointerData.touchingIdBits, | 
|  | 1583 | mViewport.displayId); | 
|  | 1584 | } | 
|  | 1585 |  | 
|  | 1586 | if (!mCurrentMotionAborted) { | 
|  | 1587 | dispatchButtonRelease(when, policyFlags); | 
|  | 1588 | dispatchHoverExit(when, policyFlags); | 
|  | 1589 | dispatchTouches(when, policyFlags); | 
|  | 1590 | dispatchHoverEnterAndMove(when, policyFlags); | 
|  | 1591 | dispatchButtonPress(when, policyFlags); | 
|  | 1592 | } | 
|  | 1593 |  | 
|  | 1594 | if (mCurrentCookedState.cookedPointerData.pointerCount == 0) { | 
|  | 1595 | mCurrentMotionAborted = false; | 
|  | 1596 | } | 
|  | 1597 | } | 
|  | 1598 |  | 
|  | 1599 | // Synthesize key up from raw buttons if needed. | 
|  | 1600 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, | 
|  | 1601 | mViewport.displayId, policyFlags, mLastCookedState.buttonState, | 
|  | 1602 | mCurrentCookedState.buttonState); | 
|  | 1603 |  | 
|  | 1604 | // Clear some transient state. | 
|  | 1605 | mCurrentRawState.rawVScroll = 0; | 
|  | 1606 | mCurrentRawState.rawHScroll = 0; | 
|  | 1607 |  | 
|  | 1608 | // Copy current touch to last touch in preparation for the next cycle. | 
|  | 1609 | mLastRawState.copyFrom(mCurrentRawState); | 
|  | 1610 | mLastCookedState.copyFrom(mCurrentCookedState); | 
|  | 1611 | } | 
|  | 1612 |  | 
|  | 1613 | void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) { | 
|  | 1614 | if (mDeviceMode == DEVICE_MODE_DIRECT && hasExternalStylus() && mExternalStylusId != -1) { | 
|  | 1615 | mCurrentRawState.buttonState |= mExternalStylusState.buttons; | 
|  | 1616 | } | 
|  | 1617 | } | 
|  | 1618 |  | 
|  | 1619 | void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) { | 
|  | 1620 | CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData; | 
|  | 1621 | const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData; | 
|  | 1622 |  | 
|  | 1623 | if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) { | 
|  | 1624 | float pressure = mExternalStylusState.pressure; | 
|  | 1625 | if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) { | 
|  | 1626 | const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId); | 
|  | 1627 | pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE); | 
|  | 1628 | } | 
|  | 1629 | PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId); | 
|  | 1630 | coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); | 
|  | 1631 |  | 
|  | 1632 | PointerProperties& properties = | 
|  | 1633 | currentPointerData.editPointerPropertiesWithId(mExternalStylusId); | 
|  | 1634 | if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { | 
|  | 1635 | properties.toolType = mExternalStylusState.toolType; | 
|  | 1636 | } | 
|  | 1637 | } | 
|  | 1638 | } | 
|  | 1639 |  | 
|  | 1640 | bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) { | 
|  | 1641 | if (mDeviceMode != DEVICE_MODE_DIRECT || !hasExternalStylus()) { | 
|  | 1642 | return false; | 
|  | 1643 | } | 
|  | 1644 |  | 
|  | 1645 | const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 && | 
|  | 1646 | state.rawPointerData.pointerCount != 0; | 
|  | 1647 | if (initialDown) { | 
|  | 1648 | if (mExternalStylusState.pressure != 0.0f) { | 
|  | 1649 | #if DEBUG_STYLUS_FUSION | 
|  | 1650 | ALOGD("Have both stylus and touch data, beginning fusion"); | 
|  | 1651 | #endif | 
|  | 1652 | mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit(); | 
|  | 1653 | } else if (timeout) { | 
|  | 1654 | #if DEBUG_STYLUS_FUSION | 
|  | 1655 | ALOGD("Timeout expired, assuming touch is not a stylus."); | 
|  | 1656 | #endif | 
|  | 1657 | resetExternalStylus(); | 
|  | 1658 | } else { | 
|  | 1659 | if (mExternalStylusFusionTimeout == LLONG_MAX) { | 
|  | 1660 | mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT; | 
|  | 1661 | } | 
|  | 1662 | #if DEBUG_STYLUS_FUSION | 
|  | 1663 | ALOGD("No stylus data but stylus is connected, requesting timeout " | 
|  | 1664 | "(%" PRId64 "ms)", | 
|  | 1665 | mExternalStylusFusionTimeout); | 
|  | 1666 | #endif | 
|  | 1667 | getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout); | 
|  | 1668 | return true; | 
|  | 1669 | } | 
|  | 1670 | } | 
|  | 1671 |  | 
|  | 1672 | // Check if the stylus pointer has gone up. | 
|  | 1673 | if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) { | 
|  | 1674 | #if DEBUG_STYLUS_FUSION | 
|  | 1675 | ALOGD("Stylus pointer is going up"); | 
|  | 1676 | #endif | 
|  | 1677 | mExternalStylusId = -1; | 
|  | 1678 | } | 
|  | 1679 |  | 
|  | 1680 | return false; | 
|  | 1681 | } | 
|  | 1682 |  | 
|  | 1683 | void TouchInputMapper::timeoutExpired(nsecs_t when) { | 
|  | 1684 | if (mDeviceMode == DEVICE_MODE_POINTER) { | 
|  | 1685 | if (mPointerUsage == POINTER_USAGE_GESTURES) { | 
|  | 1686 | dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); | 
|  | 1687 | } | 
|  | 1688 | } else if (mDeviceMode == DEVICE_MODE_DIRECT) { | 
|  | 1689 | if (mExternalStylusFusionTimeout < when) { | 
|  | 1690 | processRawTouches(true /*timeout*/); | 
|  | 1691 | } else if (mExternalStylusFusionTimeout != LLONG_MAX) { | 
|  | 1692 | getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout); | 
|  | 1693 | } | 
|  | 1694 | } | 
|  | 1695 | } | 
|  | 1696 |  | 
|  | 1697 | void TouchInputMapper::updateExternalStylusState(const StylusState& state) { | 
|  | 1698 | mExternalStylusState.copyFrom(state); | 
|  | 1699 | if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) { | 
|  | 1700 | // We're either in the middle of a fused stream of data or we're waiting on data before | 
|  | 1701 | // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus | 
|  | 1702 | // data. | 
|  | 1703 | mExternalStylusDataPending = true; | 
|  | 1704 | processRawTouches(false /*timeout*/); | 
|  | 1705 | } | 
|  | 1706 | } | 
|  | 1707 |  | 
|  | 1708 | bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) { | 
|  | 1709 | // Check for release of a virtual key. | 
|  | 1710 | if (mCurrentVirtualKey.down) { | 
|  | 1711 | if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) { | 
|  | 1712 | // Pointer went up while virtual key was down. | 
|  | 1713 | mCurrentVirtualKey.down = false; | 
|  | 1714 | if (!mCurrentVirtualKey.ignored) { | 
|  | 1715 | #if DEBUG_VIRTUAL_KEYS | 
|  | 1716 | ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", | 
|  | 1717 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); | 
|  | 1718 | #endif | 
|  | 1719 | dispatchVirtualKey(when, policyFlags, AKEY_EVENT_ACTION_UP, | 
|  | 1720 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); | 
|  | 1721 | } | 
|  | 1722 | return true; | 
|  | 1723 | } | 
|  | 1724 |  | 
|  | 1725 | if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) { | 
|  | 1726 | uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit(); | 
|  | 1727 | const RawPointerData::Pointer& pointer = | 
|  | 1728 | mCurrentRawState.rawPointerData.pointerForId(id); | 
|  | 1729 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); | 
|  | 1730 | if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) { | 
|  | 1731 | // Pointer is still within the space of the virtual key. | 
|  | 1732 | return true; | 
|  | 1733 | } | 
|  | 1734 | } | 
|  | 1735 |  | 
|  | 1736 | // Pointer left virtual key area or another pointer also went down. | 
|  | 1737 | // Send key cancellation but do not consume the touch yet. | 
|  | 1738 | // This is useful when the user swipes through from the virtual key area | 
|  | 1739 | // into the main display surface. | 
|  | 1740 | mCurrentVirtualKey.down = false; | 
|  | 1741 | if (!mCurrentVirtualKey.ignored) { | 
|  | 1742 | #if DEBUG_VIRTUAL_KEYS | 
|  | 1743 | ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", mCurrentVirtualKey.keyCode, | 
|  | 1744 | mCurrentVirtualKey.scanCode); | 
|  | 1745 | #endif | 
|  | 1746 | dispatchVirtualKey(when, policyFlags, AKEY_EVENT_ACTION_UP, | 
|  | 1747 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY | | 
|  | 1748 | AKEY_EVENT_FLAG_CANCELED); | 
|  | 1749 | } | 
|  | 1750 | } | 
|  | 1751 |  | 
|  | 1752 | if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() && | 
|  | 1753 | !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) { | 
|  | 1754 | // Pointer just went down.  Check for virtual key press or off-screen touches. | 
|  | 1755 | uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit(); | 
|  | 1756 | const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id); | 
|  | 1757 | if (!isPointInsideSurface(pointer.x, pointer.y)) { | 
|  | 1758 | // If exactly one pointer went down, check for virtual key hit. | 
|  | 1759 | // Otherwise we will drop the entire stroke. | 
|  | 1760 | if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) { | 
|  | 1761 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); | 
|  | 1762 | if (virtualKey) { | 
|  | 1763 | mCurrentVirtualKey.down = true; | 
|  | 1764 | mCurrentVirtualKey.downTime = when; | 
|  | 1765 | mCurrentVirtualKey.keyCode = virtualKey->keyCode; | 
|  | 1766 | mCurrentVirtualKey.scanCode = virtualKey->scanCode; | 
|  | 1767 | mCurrentVirtualKey.ignored = | 
|  | 1768 | mContext->shouldDropVirtualKey(when, getDevice(), virtualKey->keyCode, | 
|  | 1769 | virtualKey->scanCode); | 
|  | 1770 |  | 
|  | 1771 | if (!mCurrentVirtualKey.ignored) { | 
|  | 1772 | #if DEBUG_VIRTUAL_KEYS | 
|  | 1773 | ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", | 
|  | 1774 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); | 
|  | 1775 | #endif | 
|  | 1776 | dispatchVirtualKey(when, policyFlags, AKEY_EVENT_ACTION_DOWN, | 
|  | 1777 | AKEY_EVENT_FLAG_FROM_SYSTEM | | 
|  | 1778 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); | 
|  | 1779 | } | 
|  | 1780 | } | 
|  | 1781 | } | 
|  | 1782 | return true; | 
|  | 1783 | } | 
|  | 1784 | } | 
|  | 1785 |  | 
|  | 1786 | // Disable all virtual key touches that happen within a short time interval of the | 
|  | 1787 | // most recent touch within the screen area.  The idea is to filter out stray | 
|  | 1788 | // virtual key presses when interacting with the touch screen. | 
|  | 1789 | // | 
|  | 1790 | // Problems we're trying to solve: | 
|  | 1791 | // | 
|  | 1792 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a | 
|  | 1793 | //    virtual key area that is implemented by a separate touch panel and accidentally | 
|  | 1794 | //    triggers a virtual key. | 
|  | 1795 | // | 
|  | 1796 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen | 
|  | 1797 | //    area and accidentally triggers a virtual key.  This often happens when virtual keys | 
|  | 1798 | //    are layed out below the screen near to where the on screen keyboard's space bar | 
|  | 1799 | //    is displayed. | 
|  | 1800 | if (mConfig.virtualKeyQuietTime > 0 && | 
|  | 1801 | !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) { | 
|  | 1802 | mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); | 
|  | 1803 | } | 
|  | 1804 | return false; | 
|  | 1805 | } | 
|  | 1806 |  | 
|  | 1807 | void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, | 
|  | 1808 | int32_t keyEventAction, int32_t keyEventFlags) { | 
|  | 1809 | int32_t keyCode = mCurrentVirtualKey.keyCode; | 
|  | 1810 | int32_t scanCode = mCurrentVirtualKey.scanCode; | 
|  | 1811 | nsecs_t downTime = mCurrentVirtualKey.downTime; | 
|  | 1812 | int32_t metaState = mContext->getGlobalMetaState(); | 
|  | 1813 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
|  | 1814 |  | 
|  | 1815 | NotifyKeyArgs args(mContext->getNextSequenceNum(), when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, | 
|  | 1816 | mViewport.displayId, policyFlags, keyEventAction, keyEventFlags, keyCode, | 
|  | 1817 | scanCode, metaState, downTime); | 
|  | 1818 | getListener()->notifyKey(&args); | 
|  | 1819 | } | 
|  | 1820 |  | 
|  | 1821 | void TouchInputMapper::abortTouches(nsecs_t when, uint32_t policyFlags) { | 
|  | 1822 | BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits; | 
|  | 1823 | if (!currentIdBits.isEmpty()) { | 
|  | 1824 | int32_t metaState = getContext()->getGlobalMetaState(); | 
|  | 1825 | int32_t buttonState = mCurrentCookedState.buttonState; | 
|  | 1826 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0, metaState, | 
|  | 1827 | buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, | 
|  | 1828 | mCurrentCookedState.cookedPointerData.pointerProperties, | 
|  | 1829 | mCurrentCookedState.cookedPointerData.pointerCoords, | 
|  | 1830 | mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1, | 
|  | 1831 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); | 
|  | 1832 | mCurrentMotionAborted = true; | 
|  | 1833 | } | 
|  | 1834 | } | 
|  | 1835 |  | 
|  | 1836 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { | 
|  | 1837 | BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits; | 
|  | 1838 | BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits; | 
|  | 1839 | int32_t metaState = getContext()->getGlobalMetaState(); | 
|  | 1840 | int32_t buttonState = mCurrentCookedState.buttonState; | 
|  | 1841 |  | 
|  | 1842 | if (currentIdBits == lastIdBits) { | 
|  | 1843 | if (!currentIdBits.isEmpty()) { | 
|  | 1844 | // No pointer id changes so this is a move event. | 
|  | 1845 | // The listener takes care of batching moves so we don't have to deal with that here. | 
|  | 1846 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, | 
|  | 1847 | buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, | 
|  | 1848 | mCurrentCookedState.cookedPointerData.pointerProperties, | 
|  | 1849 | mCurrentCookedState.cookedPointerData.pointerCoords, | 
|  | 1850 | mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, -1, | 
|  | 1851 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); | 
|  | 1852 | } | 
|  | 1853 | } else { | 
|  | 1854 | // There may be pointers going up and pointers going down and pointers moving | 
|  | 1855 | // all at the same time. | 
|  | 1856 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); | 
|  | 1857 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); | 
|  | 1858 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); | 
|  | 1859 | BitSet32 dispatchedIdBits(lastIdBits.value); | 
|  | 1860 |  | 
|  | 1861 | // Update last coordinates of pointers that have moved so that we observe the new | 
|  | 1862 | // pointer positions at the same time as other pointers that have just gone up. | 
|  | 1863 | bool moveNeeded = | 
|  | 1864 | updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties, | 
|  | 1865 | mCurrentCookedState.cookedPointerData.pointerCoords, | 
|  | 1866 | mCurrentCookedState.cookedPointerData.idToIndex, | 
|  | 1867 | mLastCookedState.cookedPointerData.pointerProperties, | 
|  | 1868 | mLastCookedState.cookedPointerData.pointerCoords, | 
|  | 1869 | mLastCookedState.cookedPointerData.idToIndex, moveIdBits); | 
|  | 1870 | if (buttonState != mLastCookedState.buttonState) { | 
|  | 1871 | moveNeeded = true; | 
|  | 1872 | } | 
|  | 1873 |  | 
|  | 1874 | // Dispatch pointer up events. | 
|  | 1875 | while (!upIdBits.isEmpty()) { | 
|  | 1876 | uint32_t upId = upIdBits.clearFirstMarkedBit(); | 
|  | 1877 |  | 
|  | 1878 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0, 0, | 
|  | 1879 | metaState, buttonState, 0, | 
|  | 1880 | mLastCookedState.cookedPointerData.pointerProperties, | 
|  | 1881 | mLastCookedState.cookedPointerData.pointerCoords, | 
|  | 1882 | mLastCookedState.cookedPointerData.idToIndex, dispatchedIdBits, upId, | 
|  | 1883 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); | 
|  | 1884 | dispatchedIdBits.clearBit(upId); | 
|  | 1885 | } | 
|  | 1886 |  | 
|  | 1887 | // Dispatch move events if any of the remaining pointers moved from their old locations. | 
|  | 1888 | // Although applications receive new locations as part of individual pointer up | 
|  | 1889 | // events, they do not generally handle them except when presented in a move event. | 
|  | 1890 | if (moveNeeded && !moveIdBits.isEmpty()) { | 
|  | 1891 | ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); | 
|  | 1892 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, | 
|  | 1893 | buttonState, 0, mCurrentCookedState.cookedPointerData.pointerProperties, | 
|  | 1894 | mCurrentCookedState.cookedPointerData.pointerCoords, | 
|  | 1895 | mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, -1, | 
|  | 1896 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); | 
|  | 1897 | } | 
|  | 1898 |  | 
|  | 1899 | // Dispatch pointer down events using the new pointer locations. | 
|  | 1900 | while (!downIdBits.isEmpty()) { | 
|  | 1901 | uint32_t downId = downIdBits.clearFirstMarkedBit(); | 
|  | 1902 | dispatchedIdBits.markBit(downId); | 
|  | 1903 |  | 
|  | 1904 | if (dispatchedIdBits.count() == 1) { | 
|  | 1905 | // First pointer is going down.  Set down time. | 
|  | 1906 | mDownTime = when; | 
|  | 1907 | } | 
|  | 1908 |  | 
|  | 1909 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN, 0, 0, | 
|  | 1910 | metaState, buttonState, 0, | 
|  | 1911 | mCurrentCookedState.cookedPointerData.pointerProperties, | 
|  | 1912 | mCurrentCookedState.cookedPointerData.pointerCoords, | 
|  | 1913 | mCurrentCookedState.cookedPointerData.idToIndex, dispatchedIdBits, | 
|  | 1914 | downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime); | 
|  | 1915 | } | 
|  | 1916 | } | 
|  | 1917 | } | 
|  | 1918 |  | 
|  | 1919 | void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) { | 
|  | 1920 | if (mSentHoverEnter && | 
|  | 1921 | (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() || | 
|  | 1922 | !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) { | 
|  | 1923 | int32_t metaState = getContext()->getGlobalMetaState(); | 
|  | 1924 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState, | 
|  | 1925 | mLastCookedState.buttonState, 0, | 
|  | 1926 | mLastCookedState.cookedPointerData.pointerProperties, | 
|  | 1927 | mLastCookedState.cookedPointerData.pointerCoords, | 
|  | 1928 | mLastCookedState.cookedPointerData.idToIndex, | 
|  | 1929 | mLastCookedState.cookedPointerData.hoveringIdBits, -1, mOrientedXPrecision, | 
|  | 1930 | mOrientedYPrecision, mDownTime); | 
|  | 1931 | mSentHoverEnter = false; | 
|  | 1932 | } | 
|  | 1933 | } | 
|  | 1934 |  | 
|  | 1935 | void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) { | 
|  | 1936 | if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() && | 
|  | 1937 | !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) { | 
|  | 1938 | int32_t metaState = getContext()->getGlobalMetaState(); | 
|  | 1939 | if (!mSentHoverEnter) { | 
|  | 1940 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, | 
|  | 1941 | metaState, mCurrentRawState.buttonState, 0, | 
|  | 1942 | mCurrentCookedState.cookedPointerData.pointerProperties, | 
|  | 1943 | mCurrentCookedState.cookedPointerData.pointerCoords, | 
|  | 1944 | mCurrentCookedState.cookedPointerData.idToIndex, | 
|  | 1945 | mCurrentCookedState.cookedPointerData.hoveringIdBits, -1, | 
|  | 1946 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); | 
|  | 1947 | mSentHoverEnter = true; | 
|  | 1948 | } | 
|  | 1949 |  | 
|  | 1950 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, metaState, | 
|  | 1951 | mCurrentRawState.buttonState, 0, | 
|  | 1952 | mCurrentCookedState.cookedPointerData.pointerProperties, | 
|  | 1953 | mCurrentCookedState.cookedPointerData.pointerCoords, | 
|  | 1954 | mCurrentCookedState.cookedPointerData.idToIndex, | 
|  | 1955 | mCurrentCookedState.cookedPointerData.hoveringIdBits, -1, | 
|  | 1956 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); | 
|  | 1957 | } | 
|  | 1958 | } | 
|  | 1959 |  | 
|  | 1960 | void TouchInputMapper::dispatchButtonRelease(nsecs_t when, uint32_t policyFlags) { | 
|  | 1961 | BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState); | 
|  | 1962 | const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData); | 
|  | 1963 | const int32_t metaState = getContext()->getGlobalMetaState(); | 
|  | 1964 | int32_t buttonState = mLastCookedState.buttonState; | 
|  | 1965 | while (!releasedButtons.isEmpty()) { | 
|  | 1966 | int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit()); | 
|  | 1967 | buttonState &= ~actionButton; | 
|  | 1968 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1969 | actionButton, 0, metaState, buttonState, 0, | 
|  | 1970 | mCurrentCookedState.cookedPointerData.pointerProperties, | 
|  | 1971 | mCurrentCookedState.cookedPointerData.pointerCoords, | 
|  | 1972 | mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1, | 
|  | 1973 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); | 
|  | 1974 | } | 
|  | 1975 | } | 
|  | 1976 |  | 
|  | 1977 | void TouchInputMapper::dispatchButtonPress(nsecs_t when, uint32_t policyFlags) { | 
|  | 1978 | BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState); | 
|  | 1979 | const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData); | 
|  | 1980 | const int32_t metaState = getContext()->getGlobalMetaState(); | 
|  | 1981 | int32_t buttonState = mLastCookedState.buttonState; | 
|  | 1982 | while (!pressedButtons.isEmpty()) { | 
|  | 1983 | int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit()); | 
|  | 1984 | buttonState |= actionButton; | 
|  | 1985 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS, actionButton, | 
|  | 1986 | 0, metaState, buttonState, 0, | 
|  | 1987 | mCurrentCookedState.cookedPointerData.pointerProperties, | 
|  | 1988 | mCurrentCookedState.cookedPointerData.pointerCoords, | 
|  | 1989 | mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1, | 
|  | 1990 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); | 
|  | 1991 | } | 
|  | 1992 | } | 
|  | 1993 |  | 
|  | 1994 | const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) { | 
|  | 1995 | if (!cookedPointerData.touchingIdBits.isEmpty()) { | 
|  | 1996 | return cookedPointerData.touchingIdBits; | 
|  | 1997 | } | 
|  | 1998 | return cookedPointerData.hoveringIdBits; | 
|  | 1999 | } | 
|  | 2000 |  | 
|  | 2001 | void TouchInputMapper::cookPointerData() { | 
|  | 2002 | uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount; | 
|  | 2003 |  | 
|  | 2004 | mCurrentCookedState.cookedPointerData.clear(); | 
|  | 2005 | mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount; | 
|  | 2006 | mCurrentCookedState.cookedPointerData.hoveringIdBits = | 
|  | 2007 | mCurrentRawState.rawPointerData.hoveringIdBits; | 
|  | 2008 | mCurrentCookedState.cookedPointerData.touchingIdBits = | 
|  | 2009 | mCurrentRawState.rawPointerData.touchingIdBits; | 
|  | 2010 |  | 
|  | 2011 | if (mCurrentCookedState.cookedPointerData.pointerCount == 0) { | 
|  | 2012 | mCurrentCookedState.buttonState = 0; | 
|  | 2013 | } else { | 
|  | 2014 | mCurrentCookedState.buttonState = mCurrentRawState.buttonState; | 
|  | 2015 | } | 
|  | 2016 |  | 
|  | 2017 | // Walk through the the active pointers and map device coordinates onto | 
|  | 2018 | // surface coordinates and adjust for display orientation. | 
|  | 2019 | for (uint32_t i = 0; i < currentPointerCount; i++) { | 
|  | 2020 | const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i]; | 
|  | 2021 |  | 
|  | 2022 | // Size | 
|  | 2023 | float touchMajor, touchMinor, toolMajor, toolMinor, size; | 
|  | 2024 | switch (mCalibration.sizeCalibration) { | 
|  | 2025 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: | 
|  | 2026 | case Calibration::SIZE_CALIBRATION_DIAMETER: | 
|  | 2027 | case Calibration::SIZE_CALIBRATION_BOX: | 
|  | 2028 | case Calibration::SIZE_CALIBRATION_AREA: | 
|  | 2029 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) { | 
|  | 2030 | touchMajor = in.touchMajor; | 
|  | 2031 | touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; | 
|  | 2032 | toolMajor = in.toolMajor; | 
|  | 2033 | toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; | 
|  | 2034 | size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor) | 
|  | 2035 | : in.touchMajor; | 
|  | 2036 | } else if (mRawPointerAxes.touchMajor.valid) { | 
|  | 2037 | toolMajor = touchMajor = in.touchMajor; | 
|  | 2038 | toolMinor = touchMinor = | 
|  | 2039 | mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; | 
|  | 2040 | size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor) | 
|  | 2041 | : in.touchMajor; | 
|  | 2042 | } else if (mRawPointerAxes.toolMajor.valid) { | 
|  | 2043 | touchMajor = toolMajor = in.toolMajor; | 
|  | 2044 | touchMinor = toolMinor = | 
|  | 2045 | mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; | 
|  | 2046 | size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor) | 
|  | 2047 | : in.toolMajor; | 
|  | 2048 | } else { | 
|  | 2049 | ALOG_ASSERT(false, | 
|  | 2050 | "No touch or tool axes.  " | 
|  | 2051 | "Size calibration should have been resolved to NONE."); | 
|  | 2052 | touchMajor = 0; | 
|  | 2053 | touchMinor = 0; | 
|  | 2054 | toolMajor = 0; | 
|  | 2055 | toolMinor = 0; | 
|  | 2056 | size = 0; | 
|  | 2057 | } | 
|  | 2058 |  | 
|  | 2059 | if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) { | 
|  | 2060 | uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count(); | 
|  | 2061 | if (touchingCount > 1) { | 
|  | 2062 | touchMajor /= touchingCount; | 
|  | 2063 | touchMinor /= touchingCount; | 
|  | 2064 | toolMajor /= touchingCount; | 
|  | 2065 | toolMinor /= touchingCount; | 
|  | 2066 | size /= touchingCount; | 
|  | 2067 | } | 
|  | 2068 | } | 
|  | 2069 |  | 
|  | 2070 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) { | 
|  | 2071 | touchMajor *= mGeometricScale; | 
|  | 2072 | touchMinor *= mGeometricScale; | 
|  | 2073 | toolMajor *= mGeometricScale; | 
|  | 2074 | toolMinor *= mGeometricScale; | 
|  | 2075 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) { | 
|  | 2076 | touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0; | 
|  | 2077 | touchMinor = touchMajor; | 
|  | 2078 | toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0; | 
|  | 2079 | toolMinor = toolMajor; | 
|  | 2080 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) { | 
|  | 2081 | touchMinor = touchMajor; | 
|  | 2082 | toolMinor = toolMajor; | 
|  | 2083 | } | 
|  | 2084 |  | 
|  | 2085 | mCalibration.applySizeScaleAndBias(&touchMajor); | 
|  | 2086 | mCalibration.applySizeScaleAndBias(&touchMinor); | 
|  | 2087 | mCalibration.applySizeScaleAndBias(&toolMajor); | 
|  | 2088 | mCalibration.applySizeScaleAndBias(&toolMinor); | 
|  | 2089 | size *= mSizeScale; | 
|  | 2090 | break; | 
|  | 2091 | default: | 
|  | 2092 | touchMajor = 0; | 
|  | 2093 | touchMinor = 0; | 
|  | 2094 | toolMajor = 0; | 
|  | 2095 | toolMinor = 0; | 
|  | 2096 | size = 0; | 
|  | 2097 | break; | 
|  | 2098 | } | 
|  | 2099 |  | 
|  | 2100 | // Pressure | 
|  | 2101 | float pressure; | 
|  | 2102 | switch (mCalibration.pressureCalibration) { | 
|  | 2103 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: | 
|  | 2104 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: | 
|  | 2105 | pressure = in.pressure * mPressureScale; | 
|  | 2106 | break; | 
|  | 2107 | default: | 
|  | 2108 | pressure = in.isHovering ? 0 : 1; | 
|  | 2109 | break; | 
|  | 2110 | } | 
|  | 2111 |  | 
|  | 2112 | // Tilt and Orientation | 
|  | 2113 | float tilt; | 
|  | 2114 | float orientation; | 
|  | 2115 | if (mHaveTilt) { | 
|  | 2116 | float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale; | 
|  | 2117 | float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale; | 
|  | 2118 | orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); | 
|  | 2119 | tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); | 
|  | 2120 | } else { | 
|  | 2121 | tilt = 0; | 
|  | 2122 |  | 
|  | 2123 | switch (mCalibration.orientationCalibration) { | 
|  | 2124 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: | 
|  | 2125 | orientation = in.orientation * mOrientationScale; | 
|  | 2126 | break; | 
|  | 2127 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { | 
|  | 2128 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); | 
|  | 2129 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); | 
|  | 2130 | if (c1 != 0 || c2 != 0) { | 
|  | 2131 | orientation = atan2f(c1, c2) * 0.5f; | 
|  | 2132 | float confidence = hypotf(c1, c2); | 
|  | 2133 | float scale = 1.0f + confidence / 16.0f; | 
|  | 2134 | touchMajor *= scale; | 
|  | 2135 | touchMinor /= scale; | 
|  | 2136 | toolMajor *= scale; | 
|  | 2137 | toolMinor /= scale; | 
|  | 2138 | } else { | 
|  | 2139 | orientation = 0; | 
|  | 2140 | } | 
|  | 2141 | break; | 
|  | 2142 | } | 
|  | 2143 | default: | 
|  | 2144 | orientation = 0; | 
|  | 2145 | } | 
|  | 2146 | } | 
|  | 2147 |  | 
|  | 2148 | // Distance | 
|  | 2149 | float distance; | 
|  | 2150 | switch (mCalibration.distanceCalibration) { | 
|  | 2151 | case Calibration::DISTANCE_CALIBRATION_SCALED: | 
|  | 2152 | distance = in.distance * mDistanceScale; | 
|  | 2153 | break; | 
|  | 2154 | default: | 
|  | 2155 | distance = 0; | 
|  | 2156 | } | 
|  | 2157 |  | 
|  | 2158 | // Coverage | 
|  | 2159 | int32_t rawLeft, rawTop, rawRight, rawBottom; | 
|  | 2160 | switch (mCalibration.coverageCalibration) { | 
|  | 2161 | case Calibration::COVERAGE_CALIBRATION_BOX: | 
|  | 2162 | rawLeft = (in.toolMinor & 0xffff0000) >> 16; | 
|  | 2163 | rawRight = in.toolMinor & 0x0000ffff; | 
|  | 2164 | rawBottom = in.toolMajor & 0x0000ffff; | 
|  | 2165 | rawTop = (in.toolMajor & 0xffff0000) >> 16; | 
|  | 2166 | break; | 
|  | 2167 | default: | 
|  | 2168 | rawLeft = rawTop = rawRight = rawBottom = 0; | 
|  | 2169 | break; | 
|  | 2170 | } | 
|  | 2171 |  | 
|  | 2172 | // Adjust X,Y coords for device calibration | 
|  | 2173 | // TODO: Adjust coverage coords? | 
|  | 2174 | float xTransformed = in.x, yTransformed = in.y; | 
|  | 2175 | mAffineTransform.applyTo(xTransformed, yTransformed); | 
|  | 2176 |  | 
|  | 2177 | // Adjust X, Y, and coverage coords for surface orientation. | 
|  | 2178 | float x, y; | 
|  | 2179 | float left, top, right, bottom; | 
|  | 2180 |  | 
|  | 2181 | switch (mSurfaceOrientation) { | 
|  | 2182 | case DISPLAY_ORIENTATION_90: | 
|  | 2183 | x = float(yTransformed - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; | 
|  | 2184 | y = float(mRawPointerAxes.x.maxValue - xTransformed) * mXScale + mXTranslate; | 
|  | 2185 | left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; | 
|  | 2186 | right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; | 
|  | 2187 | bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate; | 
|  | 2188 | top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate; | 
|  | 2189 | orientation -= M_PI_2; | 
|  | 2190 | if (mOrientedRanges.haveOrientation && | 
|  | 2191 | orientation < mOrientedRanges.orientation.min) { | 
|  | 2192 | orientation += | 
|  | 2193 | (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min); | 
|  | 2194 | } | 
|  | 2195 | break; | 
|  | 2196 | case DISPLAY_ORIENTATION_180: | 
|  | 2197 | x = float(mRawPointerAxes.x.maxValue - xTransformed) * mXScale; | 
|  | 2198 | y = float(mRawPointerAxes.y.maxValue - yTransformed) * mYScale + mYTranslate; | 
|  | 2199 | left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale; | 
|  | 2200 | right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale; | 
|  | 2201 | bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate; | 
|  | 2202 | top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate; | 
|  | 2203 | orientation -= M_PI; | 
|  | 2204 | if (mOrientedRanges.haveOrientation && | 
|  | 2205 | orientation < mOrientedRanges.orientation.min) { | 
|  | 2206 | orientation += | 
|  | 2207 | (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min); | 
|  | 2208 | } | 
|  | 2209 | break; | 
|  | 2210 | case DISPLAY_ORIENTATION_270: | 
|  | 2211 | x = float(mRawPointerAxes.y.maxValue - yTransformed) * mYScale; | 
|  | 2212 | y = float(xTransformed - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; | 
|  | 2213 | left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale; | 
|  | 2214 | right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale; | 
|  | 2215 | bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; | 
|  | 2216 | top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; | 
|  | 2217 | orientation += M_PI_2; | 
|  | 2218 | if (mOrientedRanges.haveOrientation && | 
|  | 2219 | orientation > mOrientedRanges.orientation.max) { | 
|  | 2220 | orientation -= | 
|  | 2221 | (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min); | 
|  | 2222 | } | 
|  | 2223 | break; | 
|  | 2224 | default: | 
|  | 2225 | x = float(xTransformed - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; | 
|  | 2226 | y = float(yTransformed - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; | 
|  | 2227 | left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; | 
|  | 2228 | right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; | 
|  | 2229 | bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; | 
|  | 2230 | top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; | 
|  | 2231 | break; | 
|  | 2232 | } | 
|  | 2233 |  | 
|  | 2234 | // Write output coords. | 
|  | 2235 | PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i]; | 
|  | 2236 | out.clear(); | 
|  | 2237 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); | 
|  | 2238 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); | 
|  | 2239 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); | 
|  | 2240 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); | 
|  | 2241 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); | 
|  | 2242 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); | 
|  | 2243 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); | 
|  | 2244 | out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt); | 
|  | 2245 | out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); | 
|  | 2246 | if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) { | 
|  | 2247 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left); | 
|  | 2248 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top); | 
|  | 2249 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right); | 
|  | 2250 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom); | 
|  | 2251 | } else { | 
|  | 2252 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); | 
|  | 2253 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); | 
|  | 2254 | } | 
|  | 2255 |  | 
|  | 2256 | // Write output properties. | 
|  | 2257 | PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i]; | 
|  | 2258 | uint32_t id = in.id; | 
|  | 2259 | properties.clear(); | 
|  | 2260 | properties.id = id; | 
|  | 2261 | properties.toolType = in.toolType; | 
|  | 2262 |  | 
|  | 2263 | // Write id index. | 
|  | 2264 | mCurrentCookedState.cookedPointerData.idToIndex[id] = i; | 
|  | 2265 | } | 
|  | 2266 | } | 
|  | 2267 |  | 
|  | 2268 | void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, | 
|  | 2269 | PointerUsage pointerUsage) { | 
|  | 2270 | if (pointerUsage != mPointerUsage) { | 
|  | 2271 | abortPointerUsage(when, policyFlags); | 
|  | 2272 | mPointerUsage = pointerUsage; | 
|  | 2273 | } | 
|  | 2274 |  | 
|  | 2275 | switch (mPointerUsage) { | 
|  | 2276 | case POINTER_USAGE_GESTURES: | 
|  | 2277 | dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); | 
|  | 2278 | break; | 
|  | 2279 | case POINTER_USAGE_STYLUS: | 
|  | 2280 | dispatchPointerStylus(when, policyFlags); | 
|  | 2281 | break; | 
|  | 2282 | case POINTER_USAGE_MOUSE: | 
|  | 2283 | dispatchPointerMouse(when, policyFlags); | 
|  | 2284 | break; | 
|  | 2285 | default: | 
|  | 2286 | break; | 
|  | 2287 | } | 
|  | 2288 | } | 
|  | 2289 |  | 
|  | 2290 | void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) { | 
|  | 2291 | switch (mPointerUsage) { | 
|  | 2292 | case POINTER_USAGE_GESTURES: | 
|  | 2293 | abortPointerGestures(when, policyFlags); | 
|  | 2294 | break; | 
|  | 2295 | case POINTER_USAGE_STYLUS: | 
|  | 2296 | abortPointerStylus(when, policyFlags); | 
|  | 2297 | break; | 
|  | 2298 | case POINTER_USAGE_MOUSE: | 
|  | 2299 | abortPointerMouse(when, policyFlags); | 
|  | 2300 | break; | 
|  | 2301 | default: | 
|  | 2302 | break; | 
|  | 2303 | } | 
|  | 2304 |  | 
|  | 2305 | mPointerUsage = POINTER_USAGE_NONE; | 
|  | 2306 | } | 
|  | 2307 |  | 
|  | 2308 | void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout) { | 
|  | 2309 | // Update current gesture coordinates. | 
|  | 2310 | bool cancelPreviousGesture, finishPreviousGesture; | 
|  | 2311 | bool sendEvents = | 
|  | 2312 | preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout); | 
|  | 2313 | if (!sendEvents) { | 
|  | 2314 | return; | 
|  | 2315 | } | 
|  | 2316 | if (finishPreviousGesture) { | 
|  | 2317 | cancelPreviousGesture = false; | 
|  | 2318 | } | 
|  | 2319 |  | 
|  | 2320 | // Update the pointer presentation and spots. | 
|  | 2321 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_MULTI_TOUCH) { | 
|  | 2322 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); | 
|  | 2323 | if (finishPreviousGesture || cancelPreviousGesture) { | 
|  | 2324 | mPointerController->clearSpots(); | 
|  | 2325 | } | 
|  | 2326 |  | 
|  | 2327 | if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { | 
|  | 2328 | mPointerController->setSpots(mPointerGesture.currentGestureCoords, | 
|  | 2329 | mPointerGesture.currentGestureIdToIndex, | 
|  | 2330 | mPointerGesture.currentGestureIdBits, | 
|  | 2331 | mPointerController->getDisplayId()); | 
|  | 2332 | } | 
|  | 2333 | } else { | 
|  | 2334 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); | 
|  | 2335 | } | 
|  | 2336 |  | 
|  | 2337 | // Show or hide the pointer if needed. | 
|  | 2338 | switch (mPointerGesture.currentGestureMode) { | 
|  | 2339 | case PointerGesture::NEUTRAL: | 
|  | 2340 | case PointerGesture::QUIET: | 
|  | 2341 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_MULTI_TOUCH && | 
|  | 2342 | mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) { | 
|  | 2343 | // Remind the user of where the pointer is after finishing a gesture with spots. | 
|  | 2344 | mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); | 
|  | 2345 | } | 
|  | 2346 | break; | 
|  | 2347 | case PointerGesture::TAP: | 
|  | 2348 | case PointerGesture::TAP_DRAG: | 
|  | 2349 | case PointerGesture::BUTTON_CLICK_OR_DRAG: | 
|  | 2350 | case PointerGesture::HOVER: | 
|  | 2351 | case PointerGesture::PRESS: | 
|  | 2352 | case PointerGesture::SWIPE: | 
|  | 2353 | // Unfade the pointer when the current gesture manipulates the | 
|  | 2354 | // area directly under the pointer. | 
|  | 2355 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); | 
|  | 2356 | break; | 
|  | 2357 | case PointerGesture::FREEFORM: | 
|  | 2358 | // Fade the pointer when the current gesture manipulates a different | 
|  | 2359 | // area and there are spots to guide the user experience. | 
|  | 2360 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_MULTI_TOUCH) { | 
|  | 2361 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); | 
|  | 2362 | } else { | 
|  | 2363 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); | 
|  | 2364 | } | 
|  | 2365 | break; | 
|  | 2366 | } | 
|  | 2367 |  | 
|  | 2368 | // Send events! | 
|  | 2369 | int32_t metaState = getContext()->getGlobalMetaState(); | 
|  | 2370 | int32_t buttonState = mCurrentCookedState.buttonState; | 
|  | 2371 |  | 
|  | 2372 | // Update last coordinates of pointers that have moved so that we observe the new | 
|  | 2373 | // pointer positions at the same time as other pointers that have just gone up. | 
|  | 2374 | bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP || | 
|  | 2375 | mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG || | 
|  | 2376 | mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG || | 
|  | 2377 | mPointerGesture.currentGestureMode == PointerGesture::PRESS || | 
|  | 2378 | mPointerGesture.currentGestureMode == PointerGesture::SWIPE || | 
|  | 2379 | mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; | 
|  | 2380 | bool moveNeeded = false; | 
|  | 2381 | if (down && !cancelPreviousGesture && !finishPreviousGesture && | 
|  | 2382 | !mPointerGesture.lastGestureIdBits.isEmpty() && | 
|  | 2383 | !mPointerGesture.currentGestureIdBits.isEmpty()) { | 
|  | 2384 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value & | 
|  | 2385 | mPointerGesture.lastGestureIdBits.value); | 
|  | 2386 | moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, | 
|  | 2387 | mPointerGesture.currentGestureCoords, | 
|  | 2388 | mPointerGesture.currentGestureIdToIndex, | 
|  | 2389 | mPointerGesture.lastGestureProperties, | 
|  | 2390 | mPointerGesture.lastGestureCoords, | 
|  | 2391 | mPointerGesture.lastGestureIdToIndex, movedGestureIdBits); | 
|  | 2392 | if (buttonState != mLastCookedState.buttonState) { | 
|  | 2393 | moveNeeded = true; | 
|  | 2394 | } | 
|  | 2395 | } | 
|  | 2396 |  | 
|  | 2397 | // Send motion events for all pointers that went up or were canceled. | 
|  | 2398 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); | 
|  | 2399 | if (!dispatchedGestureIdBits.isEmpty()) { | 
|  | 2400 | if (cancelPreviousGesture) { | 
|  | 2401 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0, metaState, | 
|  | 2402 | buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, | 
|  | 2403 | mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords, | 
|  | 2404 | mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0, | 
|  | 2405 | mPointerGesture.downTime); | 
|  | 2406 |  | 
|  | 2407 | dispatchedGestureIdBits.clear(); | 
|  | 2408 | } else { | 
|  | 2409 | BitSet32 upGestureIdBits; | 
|  | 2410 | if (finishPreviousGesture) { | 
|  | 2411 | upGestureIdBits = dispatchedGestureIdBits; | 
|  | 2412 | } else { | 
|  | 2413 | upGestureIdBits.value = | 
|  | 2414 | dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value; | 
|  | 2415 | } | 
|  | 2416 | while (!upGestureIdBits.isEmpty()) { | 
|  | 2417 | uint32_t id = upGestureIdBits.clearFirstMarkedBit(); | 
|  | 2418 |  | 
|  | 2419 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_UP, 0, 0, | 
|  | 2420 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, | 
|  | 2421 | mPointerGesture.lastGestureProperties, | 
|  | 2422 | mPointerGesture.lastGestureCoords, | 
|  | 2423 | mPointerGesture.lastGestureIdToIndex, dispatchedGestureIdBits, id, 0, | 
|  | 2424 | 0, mPointerGesture.downTime); | 
|  | 2425 |  | 
|  | 2426 | dispatchedGestureIdBits.clearBit(id); | 
|  | 2427 | } | 
|  | 2428 | } | 
|  | 2429 | } | 
|  | 2430 |  | 
|  | 2431 | // Send motion events for all pointers that moved. | 
|  | 2432 | if (moveNeeded) { | 
|  | 2433 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, | 
|  | 2434 | buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, | 
|  | 2435 | mPointerGesture.currentGestureProperties, | 
|  | 2436 | mPointerGesture.currentGestureCoords, | 
|  | 2437 | mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, 0, 0, | 
|  | 2438 | mPointerGesture.downTime); | 
|  | 2439 | } | 
|  | 2440 |  | 
|  | 2441 | // Send motion events for all pointers that went down. | 
|  | 2442 | if (down) { | 
|  | 2443 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value & | 
|  | 2444 | ~dispatchedGestureIdBits.value); | 
|  | 2445 | while (!downGestureIdBits.isEmpty()) { | 
|  | 2446 | uint32_t id = downGestureIdBits.clearFirstMarkedBit(); | 
|  | 2447 | dispatchedGestureIdBits.markBit(id); | 
|  | 2448 |  | 
|  | 2449 | if (dispatchedGestureIdBits.count() == 1) { | 
|  | 2450 | mPointerGesture.downTime = when; | 
|  | 2451 | } | 
|  | 2452 |  | 
|  | 2453 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_POINTER_DOWN, 0, 0, | 
|  | 2454 | metaState, buttonState, 0, mPointerGesture.currentGestureProperties, | 
|  | 2455 | mPointerGesture.currentGestureCoords, | 
|  | 2456 | mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, id, 0, | 
|  | 2457 | 0, mPointerGesture.downTime); | 
|  | 2458 | } | 
|  | 2459 | } | 
|  | 2460 |  | 
|  | 2461 | // Send motion events for hover. | 
|  | 2462 | if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { | 
|  | 2463 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, metaState, | 
|  | 2464 | buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, | 
|  | 2465 | mPointerGesture.currentGestureProperties, | 
|  | 2466 | mPointerGesture.currentGestureCoords, | 
|  | 2467 | mPointerGesture.currentGestureIdToIndex, | 
|  | 2468 | mPointerGesture.currentGestureIdBits, -1, 0, 0, mPointerGesture.downTime); | 
|  | 2469 | } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) { | 
|  | 2470 | // Synthesize a hover move event after all pointers go up to indicate that | 
|  | 2471 | // the pointer is hovering again even if the user is not currently touching | 
|  | 2472 | // the touch pad.  This ensures that a view will receive a fresh hover enter | 
|  | 2473 | // event after a tap. | 
|  | 2474 | float x, y; | 
|  | 2475 | mPointerController->getPosition(&x, &y); | 
|  | 2476 |  | 
|  | 2477 | PointerProperties pointerProperties; | 
|  | 2478 | pointerProperties.clear(); | 
|  | 2479 | pointerProperties.id = 0; | 
|  | 2480 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
|  | 2481 |  | 
|  | 2482 | PointerCoords pointerCoords; | 
|  | 2483 | pointerCoords.clear(); | 
|  | 2484 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); | 
|  | 2485 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); | 
|  | 2486 |  | 
|  | 2487 | const int32_t displayId = mPointerController->getDisplayId(); | 
|  | 2488 | NotifyMotionArgs args(mContext->getNextSequenceNum(), when, getDeviceId(), mSource, | 
|  | 2489 | displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, | 
|  | 2490 | metaState, buttonState, MotionClassification::NONE, | 
|  | 2491 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords, | 
|  | 2492 | 0, 0, x, y, mPointerGesture.downTime, /* videoFrames */ {}); | 
|  | 2493 | getListener()->notifyMotion(&args); | 
|  | 2494 | } | 
|  | 2495 |  | 
|  | 2496 | // Update state. | 
|  | 2497 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; | 
|  | 2498 | if (!down) { | 
|  | 2499 | mPointerGesture.lastGestureIdBits.clear(); | 
|  | 2500 | } else { | 
|  | 2501 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; | 
|  | 2502 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) { | 
|  | 2503 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 2504 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; | 
|  | 2505 | mPointerGesture.lastGestureProperties[index].copyFrom( | 
|  | 2506 | mPointerGesture.currentGestureProperties[index]); | 
|  | 2507 | mPointerGesture.lastGestureCoords[index].copyFrom( | 
|  | 2508 | mPointerGesture.currentGestureCoords[index]); | 
|  | 2509 | mPointerGesture.lastGestureIdToIndex[id] = index; | 
|  | 2510 | } | 
|  | 2511 | } | 
|  | 2512 | } | 
|  | 2513 |  | 
|  | 2514 | void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) { | 
|  | 2515 | // Cancel previously dispatches pointers. | 
|  | 2516 | if (!mPointerGesture.lastGestureIdBits.isEmpty()) { | 
|  | 2517 | int32_t metaState = getContext()->getGlobalMetaState(); | 
|  | 2518 | int32_t buttonState = mCurrentRawState.buttonState; | 
|  | 2519 | dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0, metaState, | 
|  | 2520 | buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, | 
|  | 2521 | mPointerGesture.lastGestureProperties, mPointerGesture.lastGestureCoords, | 
|  | 2522 | mPointerGesture.lastGestureIdToIndex, mPointerGesture.lastGestureIdBits, -1, | 
|  | 2523 | 0, 0, mPointerGesture.downTime); | 
|  | 2524 | } | 
|  | 2525 |  | 
|  | 2526 | // Reset the current pointer gesture. | 
|  | 2527 | mPointerGesture.reset(); | 
|  | 2528 | mPointerVelocityControl.reset(); | 
|  | 2529 |  | 
|  | 2530 | // Remove any current spots. | 
|  | 2531 | if (mPointerController != nullptr) { | 
|  | 2532 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); | 
|  | 2533 | mPointerController->clearSpots(); | 
|  | 2534 | } | 
|  | 2535 | } | 
|  | 2536 |  | 
|  | 2537 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture, | 
|  | 2538 | bool* outFinishPreviousGesture, bool isTimeout) { | 
|  | 2539 | *outCancelPreviousGesture = false; | 
|  | 2540 | *outFinishPreviousGesture = false; | 
|  | 2541 |  | 
|  | 2542 | // Handle TAP timeout. | 
|  | 2543 | if (isTimeout) { | 
|  | 2544 | #if DEBUG_GESTURES | 
|  | 2545 | ALOGD("Gestures: Processing timeout"); | 
|  | 2546 | #endif | 
|  | 2547 |  | 
|  | 2548 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { | 
|  | 2549 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { | 
|  | 2550 | // The tap/drag timeout has not yet expired. | 
|  | 2551 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime + | 
|  | 2552 | mConfig.pointerGestureTapDragInterval); | 
|  | 2553 | } else { | 
|  | 2554 | // The tap is finished. | 
|  | 2555 | #if DEBUG_GESTURES | 
|  | 2556 | ALOGD("Gestures: TAP finished"); | 
|  | 2557 | #endif | 
|  | 2558 | *outFinishPreviousGesture = true; | 
|  | 2559 |  | 
|  | 2560 | mPointerGesture.activeGestureId = -1; | 
|  | 2561 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; | 
|  | 2562 | mPointerGesture.currentGestureIdBits.clear(); | 
|  | 2563 |  | 
|  | 2564 | mPointerVelocityControl.reset(); | 
|  | 2565 | return true; | 
|  | 2566 | } | 
|  | 2567 | } | 
|  | 2568 |  | 
|  | 2569 | // We did not handle this timeout. | 
|  | 2570 | return false; | 
|  | 2571 | } | 
|  | 2572 |  | 
|  | 2573 | const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count(); | 
|  | 2574 | const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count(); | 
|  | 2575 |  | 
|  | 2576 | // Update the velocity tracker. | 
|  | 2577 | { | 
|  | 2578 | VelocityTracker::Position positions[MAX_POINTERS]; | 
|  | 2579 | uint32_t count = 0; | 
|  | 2580 | for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty(); count++) { | 
|  | 2581 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 2582 | const RawPointerData::Pointer& pointer = | 
|  | 2583 | mCurrentRawState.rawPointerData.pointerForId(id); | 
|  | 2584 | positions[count].x = pointer.x * mPointerXMovementScale; | 
|  | 2585 | positions[count].y = pointer.y * mPointerYMovementScale; | 
|  | 2586 | } | 
|  | 2587 | mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits, | 
|  | 2588 | positions); | 
|  | 2589 | } | 
|  | 2590 |  | 
|  | 2591 | // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning | 
|  | 2592 | // to NEUTRAL, then we should not generate tap event. | 
|  | 2593 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER && | 
|  | 2594 | mPointerGesture.lastGestureMode != PointerGesture::TAP && | 
|  | 2595 | mPointerGesture.lastGestureMode != PointerGesture::TAP_DRAG) { | 
|  | 2596 | mPointerGesture.resetTap(); | 
|  | 2597 | } | 
|  | 2598 |  | 
|  | 2599 | // Pick a new active touch id if needed. | 
|  | 2600 | // Choose an arbitrary pointer that just went down, if there is one. | 
|  | 2601 | // Otherwise choose an arbitrary remaining pointer. | 
|  | 2602 | // This guarantees we always have an active touch id when there is at least one pointer. | 
|  | 2603 | // We keep the same active touch id for as long as possible. | 
|  | 2604 | int32_t lastActiveTouchId = mPointerGesture.activeTouchId; | 
|  | 2605 | int32_t activeTouchId = lastActiveTouchId; | 
|  | 2606 | if (activeTouchId < 0) { | 
|  | 2607 | if (!mCurrentCookedState.fingerIdBits.isEmpty()) { | 
|  | 2608 | activeTouchId = mPointerGesture.activeTouchId = | 
|  | 2609 | mCurrentCookedState.fingerIdBits.firstMarkedBit(); | 
|  | 2610 | mPointerGesture.firstTouchTime = when; | 
|  | 2611 | } | 
|  | 2612 | } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) { | 
|  | 2613 | if (!mCurrentCookedState.fingerIdBits.isEmpty()) { | 
|  | 2614 | activeTouchId = mPointerGesture.activeTouchId = | 
|  | 2615 | mCurrentCookedState.fingerIdBits.firstMarkedBit(); | 
|  | 2616 | } else { | 
|  | 2617 | activeTouchId = mPointerGesture.activeTouchId = -1; | 
|  | 2618 | } | 
|  | 2619 | } | 
|  | 2620 |  | 
|  | 2621 | // Determine whether we are in quiet time. | 
|  | 2622 | bool isQuietTime = false; | 
|  | 2623 | if (activeTouchId < 0) { | 
|  | 2624 | mPointerGesture.resetQuietTime(); | 
|  | 2625 | } else { | 
|  | 2626 | isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval; | 
|  | 2627 | if (!isQuietTime) { | 
|  | 2628 | if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS || | 
|  | 2629 | mPointerGesture.lastGestureMode == PointerGesture::SWIPE || | 
|  | 2630 | mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) && | 
|  | 2631 | currentFingerCount < 2) { | 
|  | 2632 | // Enter quiet time when exiting swipe or freeform state. | 
|  | 2633 | // This is to prevent accidentally entering the hover state and flinging the | 
|  | 2634 | // pointer when finishing a swipe and there is still one pointer left onscreen. | 
|  | 2635 | isQuietTime = true; | 
|  | 2636 | } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG && | 
|  | 2637 | currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) { | 
|  | 2638 | // Enter quiet time when releasing the button and there are still two or more | 
|  | 2639 | // fingers down.  This may indicate that one finger was used to press the button | 
|  | 2640 | // but it has not gone up yet. | 
|  | 2641 | isQuietTime = true; | 
|  | 2642 | } | 
|  | 2643 | if (isQuietTime) { | 
|  | 2644 | mPointerGesture.quietTime = when; | 
|  | 2645 | } | 
|  | 2646 | } | 
|  | 2647 | } | 
|  | 2648 |  | 
|  | 2649 | // Switch states based on button and pointer state. | 
|  | 2650 | if (isQuietTime) { | 
|  | 2651 | // Case 1: Quiet time. (QUIET) | 
|  | 2652 | #if DEBUG_GESTURES | 
|  | 2653 | ALOGD("Gestures: QUIET for next %0.3fms", | 
|  | 2654 | (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) * 0.000001f); | 
|  | 2655 | #endif | 
|  | 2656 | if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) { | 
|  | 2657 | *outFinishPreviousGesture = true; | 
|  | 2658 | } | 
|  | 2659 |  | 
|  | 2660 | mPointerGesture.activeGestureId = -1; | 
|  | 2661 | mPointerGesture.currentGestureMode = PointerGesture::QUIET; | 
|  | 2662 | mPointerGesture.currentGestureIdBits.clear(); | 
|  | 2663 |  | 
|  | 2664 | mPointerVelocityControl.reset(); | 
|  | 2665 | } else if (isPointerDown(mCurrentRawState.buttonState)) { | 
|  | 2666 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) | 
|  | 2667 | // The pointer follows the active touch point. | 
|  | 2668 | // Emit DOWN, MOVE, UP events at the pointer location. | 
|  | 2669 | // | 
|  | 2670 | // Only the active touch matters; other fingers are ignored.  This policy helps | 
|  | 2671 | // to handle the case where the user places a second finger on the touch pad | 
|  | 2672 | // to apply the necessary force to depress an integrated button below the surface. | 
|  | 2673 | // We don't want the second finger to be delivered to applications. | 
|  | 2674 | // | 
|  | 2675 | // For this to work well, we need to make sure to track the pointer that is really | 
|  | 2676 | // active.  If the user first puts one finger down to click then adds another | 
|  | 2677 | // finger to drag then the active pointer should switch to the finger that is | 
|  | 2678 | // being dragged. | 
|  | 2679 | #if DEBUG_GESTURES | 
|  | 2680 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " | 
|  | 2681 | "currentFingerCount=%d", | 
|  | 2682 | activeTouchId, currentFingerCount); | 
|  | 2683 | #endif | 
|  | 2684 | // Reset state when just starting. | 
|  | 2685 | if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { | 
|  | 2686 | *outFinishPreviousGesture = true; | 
|  | 2687 | mPointerGesture.activeGestureId = 0; | 
|  | 2688 | } | 
|  | 2689 |  | 
|  | 2690 | // Switch pointers if needed. | 
|  | 2691 | // Find the fastest pointer and follow it. | 
|  | 2692 | if (activeTouchId >= 0 && currentFingerCount > 1) { | 
|  | 2693 | int32_t bestId = -1; | 
|  | 2694 | float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; | 
|  | 2695 | for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) { | 
|  | 2696 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 2697 | float vx, vy; | 
|  | 2698 | if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { | 
|  | 2699 | float speed = hypotf(vx, vy); | 
|  | 2700 | if (speed > bestSpeed) { | 
|  | 2701 | bestId = id; | 
|  | 2702 | bestSpeed = speed; | 
|  | 2703 | } | 
|  | 2704 | } | 
|  | 2705 | } | 
|  | 2706 | if (bestId >= 0 && bestId != activeTouchId) { | 
|  | 2707 | mPointerGesture.activeTouchId = activeTouchId = bestId; | 
|  | 2708 | #if DEBUG_GESTURES | 
|  | 2709 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " | 
|  | 2710 | "bestId=%d, bestSpeed=%0.3f", | 
|  | 2711 | bestId, bestSpeed); | 
|  | 2712 | #endif | 
|  | 2713 | } | 
|  | 2714 | } | 
|  | 2715 |  | 
|  | 2716 | float deltaX = 0, deltaY = 0; | 
|  | 2717 | if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) { | 
|  | 2718 | const RawPointerData::Pointer& currentPointer = | 
|  | 2719 | mCurrentRawState.rawPointerData.pointerForId(activeTouchId); | 
|  | 2720 | const RawPointerData::Pointer& lastPointer = | 
|  | 2721 | mLastRawState.rawPointerData.pointerForId(activeTouchId); | 
|  | 2722 | deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; | 
|  | 2723 | deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; | 
|  | 2724 |  | 
|  | 2725 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); | 
|  | 2726 | mPointerVelocityControl.move(when, &deltaX, &deltaY); | 
|  | 2727 |  | 
|  | 2728 | // Move the pointer using a relative motion. | 
|  | 2729 | // When using spots, the click will occur at the position of the anchor | 
|  | 2730 | // spot and all other spots will move there. | 
|  | 2731 | mPointerController->move(deltaX, deltaY); | 
|  | 2732 | } else { | 
|  | 2733 | mPointerVelocityControl.reset(); | 
|  | 2734 | } | 
|  | 2735 |  | 
|  | 2736 | float x, y; | 
|  | 2737 | mPointerController->getPosition(&x, &y); | 
|  | 2738 |  | 
|  | 2739 | mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; | 
|  | 2740 | mPointerGesture.currentGestureIdBits.clear(); | 
|  | 2741 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); | 
|  | 2742 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; | 
|  | 2743 | mPointerGesture.currentGestureProperties[0].clear(); | 
|  | 2744 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; | 
|  | 2745 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
|  | 2746 | mPointerGesture.currentGestureCoords[0].clear(); | 
|  | 2747 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); | 
|  | 2748 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); | 
|  | 2749 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); | 
|  | 2750 | } else if (currentFingerCount == 0) { | 
|  | 2751 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) | 
|  | 2752 | if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) { | 
|  | 2753 | *outFinishPreviousGesture = true; | 
|  | 2754 | } | 
|  | 2755 |  | 
|  | 2756 | // Watch for taps coming out of HOVER or TAP_DRAG mode. | 
|  | 2757 | // Checking for taps after TAP_DRAG allows us to detect double-taps. | 
|  | 2758 | bool tapped = false; | 
|  | 2759 | if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER || | 
|  | 2760 | mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) && | 
|  | 2761 | lastFingerCount == 1) { | 
|  | 2762 | if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { | 
|  | 2763 | float x, y; | 
|  | 2764 | mPointerController->getPosition(&x, &y); | 
|  | 2765 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop && | 
|  | 2766 | fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { | 
|  | 2767 | #if DEBUG_GESTURES | 
|  | 2768 | ALOGD("Gestures: TAP"); | 
|  | 2769 | #endif | 
|  | 2770 |  | 
|  | 2771 | mPointerGesture.tapUpTime = when; | 
|  | 2772 | getContext()->requestTimeoutAtTime(when + | 
|  | 2773 | mConfig.pointerGestureTapDragInterval); | 
|  | 2774 |  | 
|  | 2775 | mPointerGesture.activeGestureId = 0; | 
|  | 2776 | mPointerGesture.currentGestureMode = PointerGesture::TAP; | 
|  | 2777 | mPointerGesture.currentGestureIdBits.clear(); | 
|  | 2778 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); | 
|  | 2779 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; | 
|  | 2780 | mPointerGesture.currentGestureProperties[0].clear(); | 
|  | 2781 | mPointerGesture.currentGestureProperties[0].id = | 
|  | 2782 | mPointerGesture.activeGestureId; | 
|  | 2783 | mPointerGesture.currentGestureProperties[0].toolType = | 
|  | 2784 | AMOTION_EVENT_TOOL_TYPE_FINGER; | 
|  | 2785 | mPointerGesture.currentGestureCoords[0].clear(); | 
|  | 2786 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, | 
|  | 2787 | mPointerGesture.tapX); | 
|  | 2788 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, | 
|  | 2789 | mPointerGesture.tapY); | 
|  | 2790 | mPointerGesture.currentGestureCoords[0] | 
|  | 2791 | .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); | 
|  | 2792 |  | 
|  | 2793 | tapped = true; | 
|  | 2794 | } else { | 
|  | 2795 | #if DEBUG_GESTURES | 
|  | 2796 | ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", x - mPointerGesture.tapX, | 
|  | 2797 | y - mPointerGesture.tapY); | 
|  | 2798 | #endif | 
|  | 2799 | } | 
|  | 2800 | } else { | 
|  | 2801 | #if DEBUG_GESTURES | 
|  | 2802 | if (mPointerGesture.tapDownTime != LLONG_MIN) { | 
|  | 2803 | ALOGD("Gestures: Not a TAP, %0.3fms since down", | 
|  | 2804 | (when - mPointerGesture.tapDownTime) * 0.000001f); | 
|  | 2805 | } else { | 
|  | 2806 | ALOGD("Gestures: Not a TAP, incompatible mode transitions"); | 
|  | 2807 | } | 
|  | 2808 | #endif | 
|  | 2809 | } | 
|  | 2810 | } | 
|  | 2811 |  | 
|  | 2812 | mPointerVelocityControl.reset(); | 
|  | 2813 |  | 
|  | 2814 | if (!tapped) { | 
|  | 2815 | #if DEBUG_GESTURES | 
|  | 2816 | ALOGD("Gestures: NEUTRAL"); | 
|  | 2817 | #endif | 
|  | 2818 | mPointerGesture.activeGestureId = -1; | 
|  | 2819 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; | 
|  | 2820 | mPointerGesture.currentGestureIdBits.clear(); | 
|  | 2821 | } | 
|  | 2822 | } else if (currentFingerCount == 1) { | 
|  | 2823 | // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG) | 
|  | 2824 | // The pointer follows the active touch point. | 
|  | 2825 | // When in HOVER, emit HOVER_MOVE events at the pointer location. | 
|  | 2826 | // When in TAP_DRAG, emit MOVE events at the pointer location. | 
|  | 2827 | ALOG_ASSERT(activeTouchId >= 0); | 
|  | 2828 |  | 
|  | 2829 | mPointerGesture.currentGestureMode = PointerGesture::HOVER; | 
|  | 2830 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { | 
|  | 2831 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { | 
|  | 2832 | float x, y; | 
|  | 2833 | mPointerController->getPosition(&x, &y); | 
|  | 2834 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop && | 
|  | 2835 | fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { | 
|  | 2836 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; | 
|  | 2837 | } else { | 
|  | 2838 | #if DEBUG_GESTURES | 
|  | 2839 | ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", | 
|  | 2840 | x - mPointerGesture.tapX, y - mPointerGesture.tapY); | 
|  | 2841 | #endif | 
|  | 2842 | } | 
|  | 2843 | } else { | 
|  | 2844 | #if DEBUG_GESTURES | 
|  | 2845 | ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", | 
|  | 2846 | (when - mPointerGesture.tapUpTime) * 0.000001f); | 
|  | 2847 | #endif | 
|  | 2848 | } | 
|  | 2849 | } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { | 
|  | 2850 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; | 
|  | 2851 | } | 
|  | 2852 |  | 
|  | 2853 | float deltaX = 0, deltaY = 0; | 
|  | 2854 | if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) { | 
|  | 2855 | const RawPointerData::Pointer& currentPointer = | 
|  | 2856 | mCurrentRawState.rawPointerData.pointerForId(activeTouchId); | 
|  | 2857 | const RawPointerData::Pointer& lastPointer = | 
|  | 2858 | mLastRawState.rawPointerData.pointerForId(activeTouchId); | 
|  | 2859 | deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; | 
|  | 2860 | deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; | 
|  | 2861 |  | 
|  | 2862 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); | 
|  | 2863 | mPointerVelocityControl.move(when, &deltaX, &deltaY); | 
|  | 2864 |  | 
|  | 2865 | // Move the pointer using a relative motion. | 
|  | 2866 | // When using spots, the hover or drag will occur at the position of the anchor spot. | 
|  | 2867 | mPointerController->move(deltaX, deltaY); | 
|  | 2868 | } else { | 
|  | 2869 | mPointerVelocityControl.reset(); | 
|  | 2870 | } | 
|  | 2871 |  | 
|  | 2872 | bool down; | 
|  | 2873 | if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { | 
|  | 2874 | #if DEBUG_GESTURES | 
|  | 2875 | ALOGD("Gestures: TAP_DRAG"); | 
|  | 2876 | #endif | 
|  | 2877 | down = true; | 
|  | 2878 | } else { | 
|  | 2879 | #if DEBUG_GESTURES | 
|  | 2880 | ALOGD("Gestures: HOVER"); | 
|  | 2881 | #endif | 
|  | 2882 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) { | 
|  | 2883 | *outFinishPreviousGesture = true; | 
|  | 2884 | } | 
|  | 2885 | mPointerGesture.activeGestureId = 0; | 
|  | 2886 | down = false; | 
|  | 2887 | } | 
|  | 2888 |  | 
|  | 2889 | float x, y; | 
|  | 2890 | mPointerController->getPosition(&x, &y); | 
|  | 2891 |  | 
|  | 2892 | mPointerGesture.currentGestureIdBits.clear(); | 
|  | 2893 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); | 
|  | 2894 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; | 
|  | 2895 | mPointerGesture.currentGestureProperties[0].clear(); | 
|  | 2896 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; | 
|  | 2897 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
|  | 2898 | mPointerGesture.currentGestureCoords[0].clear(); | 
|  | 2899 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); | 
|  | 2900 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); | 
|  | 2901 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, | 
|  | 2902 | down ? 1.0f : 0.0f); | 
|  | 2903 |  | 
|  | 2904 | if (lastFingerCount == 0 && currentFingerCount != 0) { | 
|  | 2905 | mPointerGesture.resetTap(); | 
|  | 2906 | mPointerGesture.tapDownTime = when; | 
|  | 2907 | mPointerGesture.tapX = x; | 
|  | 2908 | mPointerGesture.tapY = y; | 
|  | 2909 | } | 
|  | 2910 | } else { | 
|  | 2911 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) | 
|  | 2912 | // We need to provide feedback for each finger that goes down so we cannot wait | 
|  | 2913 | // for the fingers to move before deciding what to do. | 
|  | 2914 | // | 
|  | 2915 | // The ambiguous case is deciding what to do when there are two fingers down but they | 
|  | 2916 | // have not moved enough to determine whether they are part of a drag or part of a | 
|  | 2917 | // freeform gesture, or just a press or long-press at the pointer location. | 
|  | 2918 | // | 
|  | 2919 | // When there are two fingers we start with the PRESS hypothesis and we generate a | 
|  | 2920 | // down at the pointer location. | 
|  | 2921 | // | 
|  | 2922 | // When the two fingers move enough or when additional fingers are added, we make | 
|  | 2923 | // a decision to transition into SWIPE or FREEFORM mode accordingly. | 
|  | 2924 | ALOG_ASSERT(activeTouchId >= 0); | 
|  | 2925 |  | 
|  | 2926 | bool settled = when >= | 
|  | 2927 | mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval; | 
|  | 2928 | if (mPointerGesture.lastGestureMode != PointerGesture::PRESS && | 
|  | 2929 | mPointerGesture.lastGestureMode != PointerGesture::SWIPE && | 
|  | 2930 | mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { | 
|  | 2931 | *outFinishPreviousGesture = true; | 
|  | 2932 | } else if (!settled && currentFingerCount > lastFingerCount) { | 
|  | 2933 | // Additional pointers have gone down but not yet settled. | 
|  | 2934 | // Reset the gesture. | 
|  | 2935 | #if DEBUG_GESTURES | 
|  | 2936 | ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " | 
|  | 2937 | "settle time remaining %0.3fms", | 
|  | 2938 | (mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval - | 
|  | 2939 | when) * 0.000001f); | 
|  | 2940 | #endif | 
|  | 2941 | *outCancelPreviousGesture = true; | 
|  | 2942 | } else { | 
|  | 2943 | // Continue previous gesture. | 
|  | 2944 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; | 
|  | 2945 | } | 
|  | 2946 |  | 
|  | 2947 | if (*outFinishPreviousGesture || *outCancelPreviousGesture) { | 
|  | 2948 | mPointerGesture.currentGestureMode = PointerGesture::PRESS; | 
|  | 2949 | mPointerGesture.activeGestureId = 0; | 
|  | 2950 | mPointerGesture.referenceIdBits.clear(); | 
|  | 2951 | mPointerVelocityControl.reset(); | 
|  | 2952 |  | 
|  | 2953 | // Use the centroid and pointer location as the reference points for the gesture. | 
|  | 2954 | #if DEBUG_GESTURES | 
|  | 2955 | ALOGD("Gestures: Using centroid as reference for MULTITOUCH, " | 
|  | 2956 | "settle time remaining %0.3fms", | 
|  | 2957 | (mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval - | 
|  | 2958 | when) * 0.000001f); | 
|  | 2959 | #endif | 
|  | 2960 | mCurrentRawState.rawPointerData | 
|  | 2961 | .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX, | 
|  | 2962 | &mPointerGesture.referenceTouchY); | 
|  | 2963 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, | 
|  | 2964 | &mPointerGesture.referenceGestureY); | 
|  | 2965 | } | 
|  | 2966 |  | 
|  | 2967 | // Clear the reference deltas for fingers not yet included in the reference calculation. | 
|  | 2968 | for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value & | 
|  | 2969 | ~mPointerGesture.referenceIdBits.value); | 
|  | 2970 | !idBits.isEmpty();) { | 
|  | 2971 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 2972 | mPointerGesture.referenceDeltas[id].dx = 0; | 
|  | 2973 | mPointerGesture.referenceDeltas[id].dy = 0; | 
|  | 2974 | } | 
|  | 2975 | mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits; | 
|  | 2976 |  | 
|  | 2977 | // Add delta for all fingers and calculate a common movement delta. | 
|  | 2978 | float commonDeltaX = 0, commonDeltaY = 0; | 
|  | 2979 | BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value & | 
|  | 2980 | mCurrentCookedState.fingerIdBits.value); | 
|  | 2981 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) { | 
|  | 2982 | bool first = (idBits == commonIdBits); | 
|  | 2983 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 2984 | const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id); | 
|  | 2985 | const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id); | 
|  | 2986 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; | 
|  | 2987 | delta.dx += cpd.x - lpd.x; | 
|  | 2988 | delta.dy += cpd.y - lpd.y; | 
|  | 2989 |  | 
|  | 2990 | if (first) { | 
|  | 2991 | commonDeltaX = delta.dx; | 
|  | 2992 | commonDeltaY = delta.dy; | 
|  | 2993 | } else { | 
|  | 2994 | commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); | 
|  | 2995 | commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); | 
|  | 2996 | } | 
|  | 2997 | } | 
|  | 2998 |  | 
|  | 2999 | // Consider transitions from PRESS to SWIPE or MULTITOUCH. | 
|  | 3000 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { | 
|  | 3001 | float dist[MAX_POINTER_ID + 1]; | 
|  | 3002 | int32_t distOverThreshold = 0; | 
|  | 3003 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) { | 
|  | 3004 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 3005 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; | 
|  | 3006 | dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale); | 
|  | 3007 | if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { | 
|  | 3008 | distOverThreshold += 1; | 
|  | 3009 | } | 
|  | 3010 | } | 
|  | 3011 |  | 
|  | 3012 | // Only transition when at least two pointers have moved further than | 
|  | 3013 | // the minimum distance threshold. | 
|  | 3014 | if (distOverThreshold >= 2) { | 
|  | 3015 | if (currentFingerCount > 2) { | 
|  | 3016 | // There are more than two pointers, switch to FREEFORM. | 
|  | 3017 | #if DEBUG_GESTURES | 
|  | 3018 | ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", | 
|  | 3019 | currentFingerCount); | 
|  | 3020 | #endif | 
|  | 3021 | *outCancelPreviousGesture = true; | 
|  | 3022 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; | 
|  | 3023 | } else { | 
|  | 3024 | // There are exactly two pointers. | 
|  | 3025 | BitSet32 idBits(mCurrentCookedState.fingerIdBits); | 
|  | 3026 | uint32_t id1 = idBits.clearFirstMarkedBit(); | 
|  | 3027 | uint32_t id2 = idBits.firstMarkedBit(); | 
|  | 3028 | const RawPointerData::Pointer& p1 = | 
|  | 3029 | mCurrentRawState.rawPointerData.pointerForId(id1); | 
|  | 3030 | const RawPointerData::Pointer& p2 = | 
|  | 3031 | mCurrentRawState.rawPointerData.pointerForId(id2); | 
|  | 3032 | float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y); | 
|  | 3033 | if (mutualDistance > mPointerGestureMaxSwipeWidth) { | 
|  | 3034 | // There are two pointers but they are too far apart for a SWIPE, | 
|  | 3035 | // switch to FREEFORM. | 
|  | 3036 | #if DEBUG_GESTURES | 
|  | 3037 | ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", | 
|  | 3038 | mutualDistance, mPointerGestureMaxSwipeWidth); | 
|  | 3039 | #endif | 
|  | 3040 | *outCancelPreviousGesture = true; | 
|  | 3041 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; | 
|  | 3042 | } else { | 
|  | 3043 | // There are two pointers.  Wait for both pointers to start moving | 
|  | 3044 | // before deciding whether this is a SWIPE or FREEFORM gesture. | 
|  | 3045 | float dist1 = dist[id1]; | 
|  | 3046 | float dist2 = dist[id2]; | 
|  | 3047 | if (dist1 >= mConfig.pointerGestureMultitouchMinDistance && | 
|  | 3048 | dist2 >= mConfig.pointerGestureMultitouchMinDistance) { | 
|  | 3049 | // Calculate the dot product of the displacement vectors. | 
|  | 3050 | // When the vectors are oriented in approximately the same direction, | 
|  | 3051 | // the angle betweeen them is near zero and the cosine of the angle | 
|  | 3052 | // approches 1.0.  Recall that dot(v1, v2) = cos(angle) * mag(v1) * | 
|  | 3053 | // mag(v2). | 
|  | 3054 | PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; | 
|  | 3055 | PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; | 
|  | 3056 | float dx1 = delta1.dx * mPointerXZoomScale; | 
|  | 3057 | float dy1 = delta1.dy * mPointerYZoomScale; | 
|  | 3058 | float dx2 = delta2.dx * mPointerXZoomScale; | 
|  | 3059 | float dy2 = delta2.dy * mPointerYZoomScale; | 
|  | 3060 | float dot = dx1 * dx2 + dy1 * dy2; | 
|  | 3061 | float cosine = dot / (dist1 * dist2); // denominator always > 0 | 
|  | 3062 | if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { | 
|  | 3063 | // Pointers are moving in the same direction.  Switch to SWIPE. | 
|  | 3064 | #if DEBUG_GESTURES | 
|  | 3065 | ALOGD("Gestures: PRESS transitioned to SWIPE, " | 
|  | 3066 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " | 
|  | 3067 | "cosine %0.3f >= %0.3f", | 
|  | 3068 | dist1, mConfig.pointerGestureMultitouchMinDistance, dist2, | 
|  | 3069 | mConfig.pointerGestureMultitouchMinDistance, cosine, | 
|  | 3070 | mConfig.pointerGestureSwipeTransitionAngleCosine); | 
|  | 3071 | #endif | 
|  | 3072 | mPointerGesture.currentGestureMode = PointerGesture::SWIPE; | 
|  | 3073 | } else { | 
|  | 3074 | // Pointers are moving in different directions.  Switch to FREEFORM. | 
|  | 3075 | #if DEBUG_GESTURES | 
|  | 3076 | ALOGD("Gestures: PRESS transitioned to FREEFORM, " | 
|  | 3077 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " | 
|  | 3078 | "cosine %0.3f < %0.3f", | 
|  | 3079 | dist1, mConfig.pointerGestureMultitouchMinDistance, dist2, | 
|  | 3080 | mConfig.pointerGestureMultitouchMinDistance, cosine, | 
|  | 3081 | mConfig.pointerGestureSwipeTransitionAngleCosine); | 
|  | 3082 | #endif | 
|  | 3083 | *outCancelPreviousGesture = true; | 
|  | 3084 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; | 
|  | 3085 | } | 
|  | 3086 | } | 
|  | 3087 | } | 
|  | 3088 | } | 
|  | 3089 | } | 
|  | 3090 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { | 
|  | 3091 | // Switch from SWIPE to FREEFORM if additional pointers go down. | 
|  | 3092 | // Cancel previous gesture. | 
|  | 3093 | if (currentFingerCount > 2) { | 
|  | 3094 | #if DEBUG_GESTURES | 
|  | 3095 | ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", | 
|  | 3096 | currentFingerCount); | 
|  | 3097 | #endif | 
|  | 3098 | *outCancelPreviousGesture = true; | 
|  | 3099 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; | 
|  | 3100 | } | 
|  | 3101 | } | 
|  | 3102 |  | 
|  | 3103 | // Move the reference points based on the overall group motion of the fingers | 
|  | 3104 | // except in PRESS mode while waiting for a transition to occur. | 
|  | 3105 | if (mPointerGesture.currentGestureMode != PointerGesture::PRESS && | 
|  | 3106 | (commonDeltaX || commonDeltaY)) { | 
|  | 3107 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) { | 
|  | 3108 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 3109 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; | 
|  | 3110 | delta.dx = 0; | 
|  | 3111 | delta.dy = 0; | 
|  | 3112 | } | 
|  | 3113 |  | 
|  | 3114 | mPointerGesture.referenceTouchX += commonDeltaX; | 
|  | 3115 | mPointerGesture.referenceTouchY += commonDeltaY; | 
|  | 3116 |  | 
|  | 3117 | commonDeltaX *= mPointerXMovementScale; | 
|  | 3118 | commonDeltaY *= mPointerYMovementScale; | 
|  | 3119 |  | 
|  | 3120 | rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY); | 
|  | 3121 | mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); | 
|  | 3122 |  | 
|  | 3123 | mPointerGesture.referenceGestureX += commonDeltaX; | 
|  | 3124 | mPointerGesture.referenceGestureY += commonDeltaY; | 
|  | 3125 | } | 
|  | 3126 |  | 
|  | 3127 | // Report gestures. | 
|  | 3128 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS || | 
|  | 3129 | mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { | 
|  | 3130 | // PRESS or SWIPE mode. | 
|  | 3131 | #if DEBUG_GESTURES | 
|  | 3132 | ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d," | 
|  | 3133 | "activeGestureId=%d, currentTouchPointerCount=%d", | 
|  | 3134 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); | 
|  | 3135 | #endif | 
|  | 3136 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); | 
|  | 3137 |  | 
|  | 3138 | mPointerGesture.currentGestureIdBits.clear(); | 
|  | 3139 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); | 
|  | 3140 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; | 
|  | 3141 | mPointerGesture.currentGestureProperties[0].clear(); | 
|  | 3142 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; | 
|  | 3143 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
|  | 3144 | mPointerGesture.currentGestureCoords[0].clear(); | 
|  | 3145 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, | 
|  | 3146 | mPointerGesture.referenceGestureX); | 
|  | 3147 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, | 
|  | 3148 | mPointerGesture.referenceGestureY); | 
|  | 3149 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); | 
|  | 3150 | } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { | 
|  | 3151 | // FREEFORM mode. | 
|  | 3152 | #if DEBUG_GESTURES | 
|  | 3153 | ALOGD("Gestures: FREEFORM activeTouchId=%d," | 
|  | 3154 | "activeGestureId=%d, currentTouchPointerCount=%d", | 
|  | 3155 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); | 
|  | 3156 | #endif | 
|  | 3157 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); | 
|  | 3158 |  | 
|  | 3159 | mPointerGesture.currentGestureIdBits.clear(); | 
|  | 3160 |  | 
|  | 3161 | BitSet32 mappedTouchIdBits; | 
|  | 3162 | BitSet32 usedGestureIdBits; | 
|  | 3163 | if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { | 
|  | 3164 | // Initially, assign the active gesture id to the active touch point | 
|  | 3165 | // if there is one.  No other touch id bits are mapped yet. | 
|  | 3166 | if (!*outCancelPreviousGesture) { | 
|  | 3167 | mappedTouchIdBits.markBit(activeTouchId); | 
|  | 3168 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); | 
|  | 3169 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = | 
|  | 3170 | mPointerGesture.activeGestureId; | 
|  | 3171 | } else { | 
|  | 3172 | mPointerGesture.activeGestureId = -1; | 
|  | 3173 | } | 
|  | 3174 | } else { | 
|  | 3175 | // Otherwise, assume we mapped all touches from the previous frame. | 
|  | 3176 | // Reuse all mappings that are still applicable. | 
|  | 3177 | mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value & | 
|  | 3178 | mCurrentCookedState.fingerIdBits.value; | 
|  | 3179 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; | 
|  | 3180 |  | 
|  | 3181 | // Check whether we need to choose a new active gesture id because the | 
|  | 3182 | // current went went up. | 
|  | 3183 | for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value & | 
|  | 3184 | ~mCurrentCookedState.fingerIdBits.value); | 
|  | 3185 | !upTouchIdBits.isEmpty();) { | 
|  | 3186 | uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit(); | 
|  | 3187 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; | 
|  | 3188 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { | 
|  | 3189 | mPointerGesture.activeGestureId = -1; | 
|  | 3190 | break; | 
|  | 3191 | } | 
|  | 3192 | } | 
|  | 3193 | } | 
|  | 3194 |  | 
|  | 3195 | #if DEBUG_GESTURES | 
|  | 3196 | ALOGD("Gestures: FREEFORM follow up " | 
|  | 3197 | "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " | 
|  | 3198 | "activeGestureId=%d", | 
|  | 3199 | mappedTouchIdBits.value, usedGestureIdBits.value, | 
|  | 3200 | mPointerGesture.activeGestureId); | 
|  | 3201 | #endif | 
|  | 3202 |  | 
|  | 3203 | BitSet32 idBits(mCurrentCookedState.fingerIdBits); | 
|  | 3204 | for (uint32_t i = 0; i < currentFingerCount; i++) { | 
|  | 3205 | uint32_t touchId = idBits.clearFirstMarkedBit(); | 
|  | 3206 | uint32_t gestureId; | 
|  | 3207 | if (!mappedTouchIdBits.hasBit(touchId)) { | 
|  | 3208 | gestureId = usedGestureIdBits.markFirstUnmarkedBit(); | 
|  | 3209 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; | 
|  | 3210 | #if DEBUG_GESTURES | 
|  | 3211 | ALOGD("Gestures: FREEFORM " | 
|  | 3212 | "new mapping for touch id %d -> gesture id %d", | 
|  | 3213 | touchId, gestureId); | 
|  | 3214 | #endif | 
|  | 3215 | } else { | 
|  | 3216 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; | 
|  | 3217 | #if DEBUG_GESTURES | 
|  | 3218 | ALOGD("Gestures: FREEFORM " | 
|  | 3219 | "existing mapping for touch id %d -> gesture id %d", | 
|  | 3220 | touchId, gestureId); | 
|  | 3221 | #endif | 
|  | 3222 | } | 
|  | 3223 | mPointerGesture.currentGestureIdBits.markBit(gestureId); | 
|  | 3224 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; | 
|  | 3225 |  | 
|  | 3226 | const RawPointerData::Pointer& pointer = | 
|  | 3227 | mCurrentRawState.rawPointerData.pointerForId(touchId); | 
|  | 3228 | float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale; | 
|  | 3229 | float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale; | 
|  | 3230 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); | 
|  | 3231 |  | 
|  | 3232 | mPointerGesture.currentGestureProperties[i].clear(); | 
|  | 3233 | mPointerGesture.currentGestureProperties[i].id = gestureId; | 
|  | 3234 | mPointerGesture.currentGestureProperties[i].toolType = | 
|  | 3235 | AMOTION_EVENT_TOOL_TYPE_FINGER; | 
|  | 3236 | mPointerGesture.currentGestureCoords[i].clear(); | 
|  | 3237 | mPointerGesture.currentGestureCoords[i] | 
|  | 3238 | .setAxisValue(AMOTION_EVENT_AXIS_X, | 
|  | 3239 | mPointerGesture.referenceGestureX + deltaX); | 
|  | 3240 | mPointerGesture.currentGestureCoords[i] | 
|  | 3241 | .setAxisValue(AMOTION_EVENT_AXIS_Y, | 
|  | 3242 | mPointerGesture.referenceGestureY + deltaY); | 
|  | 3243 | mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, | 
|  | 3244 | 1.0f); | 
|  | 3245 | } | 
|  | 3246 |  | 
|  | 3247 | if (mPointerGesture.activeGestureId < 0) { | 
|  | 3248 | mPointerGesture.activeGestureId = | 
|  | 3249 | mPointerGesture.currentGestureIdBits.firstMarkedBit(); | 
|  | 3250 | #if DEBUG_GESTURES | 
|  | 3251 | ALOGD("Gestures: FREEFORM new " | 
|  | 3252 | "activeGestureId=%d", | 
|  | 3253 | mPointerGesture.activeGestureId); | 
|  | 3254 | #endif | 
|  | 3255 | } | 
|  | 3256 | } | 
|  | 3257 | } | 
|  | 3258 |  | 
|  | 3259 | mPointerController->setButtonState(mCurrentRawState.buttonState); | 
|  | 3260 |  | 
|  | 3261 | #if DEBUG_GESTURES | 
|  | 3262 | ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " | 
|  | 3263 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " | 
|  | 3264 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", | 
|  | 3265 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), | 
|  | 3266 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, | 
|  | 3267 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); | 
|  | 3268 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) { | 
|  | 3269 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 3270 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; | 
|  | 3271 | const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; | 
|  | 3272 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; | 
|  | 3273 | ALOGD("  currentGesture[%d]: index=%d, toolType=%d, " | 
|  | 3274 | "x=%0.3f, y=%0.3f, pressure=%0.3f", | 
|  | 3275 | id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 3276 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 3277 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); | 
|  | 3278 | } | 
|  | 3279 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) { | 
|  | 3280 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 3281 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; | 
|  | 3282 | const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; | 
|  | 3283 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; | 
|  | 3284 | ALOGD("  lastGesture[%d]: index=%d, toolType=%d, " | 
|  | 3285 | "x=%0.3f, y=%0.3f, pressure=%0.3f", | 
|  | 3286 | id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 3287 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 3288 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); | 
|  | 3289 | } | 
|  | 3290 | #endif | 
|  | 3291 | return true; | 
|  | 3292 | } | 
|  | 3293 |  | 
|  | 3294 | void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) { | 
|  | 3295 | mPointerSimple.currentCoords.clear(); | 
|  | 3296 | mPointerSimple.currentProperties.clear(); | 
|  | 3297 |  | 
|  | 3298 | bool down, hovering; | 
|  | 3299 | if (!mCurrentCookedState.stylusIdBits.isEmpty()) { | 
|  | 3300 | uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit(); | 
|  | 3301 | uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id]; | 
|  | 3302 | float x = mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(); | 
|  | 3303 | float y = mCurrentCookedState.cookedPointerData.pointerCoords[index].getY(); | 
|  | 3304 | mPointerController->setPosition(x, y); | 
|  | 3305 |  | 
|  | 3306 | hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id); | 
|  | 3307 | down = !hovering; | 
|  | 3308 |  | 
|  | 3309 | mPointerController->getPosition(&x, &y); | 
|  | 3310 | mPointerSimple.currentCoords.copyFrom( | 
|  | 3311 | mCurrentCookedState.cookedPointerData.pointerCoords[index]); | 
|  | 3312 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); | 
|  | 3313 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); | 
|  | 3314 | mPointerSimple.currentProperties.id = 0; | 
|  | 3315 | mPointerSimple.currentProperties.toolType = | 
|  | 3316 | mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType; | 
|  | 3317 | } else { | 
|  | 3318 | down = false; | 
|  | 3319 | hovering = false; | 
|  | 3320 | } | 
|  | 3321 |  | 
|  | 3322 | dispatchPointerSimple(when, policyFlags, down, hovering); | 
|  | 3323 | } | 
|  | 3324 |  | 
|  | 3325 | void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) { | 
|  | 3326 | abortPointerSimple(when, policyFlags); | 
|  | 3327 | } | 
|  | 3328 |  | 
|  | 3329 | void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) { | 
|  | 3330 | mPointerSimple.currentCoords.clear(); | 
|  | 3331 | mPointerSimple.currentProperties.clear(); | 
|  | 3332 |  | 
|  | 3333 | bool down, hovering; | 
|  | 3334 | if (!mCurrentCookedState.mouseIdBits.isEmpty()) { | 
|  | 3335 | uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit(); | 
|  | 3336 | uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id]; | 
|  | 3337 | float deltaX = 0, deltaY = 0; | 
|  | 3338 | if (mLastCookedState.mouseIdBits.hasBit(id)) { | 
|  | 3339 | uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id]; | 
|  | 3340 | deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x - | 
|  | 3341 | mLastRawState.rawPointerData.pointers[lastIndex].x) * | 
|  | 3342 | mPointerXMovementScale; | 
|  | 3343 | deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y - | 
|  | 3344 | mLastRawState.rawPointerData.pointers[lastIndex].y) * | 
|  | 3345 | mPointerYMovementScale; | 
|  | 3346 |  | 
|  | 3347 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); | 
|  | 3348 | mPointerVelocityControl.move(when, &deltaX, &deltaY); | 
|  | 3349 |  | 
|  | 3350 | mPointerController->move(deltaX, deltaY); | 
|  | 3351 | } else { | 
|  | 3352 | mPointerVelocityControl.reset(); | 
|  | 3353 | } | 
|  | 3354 |  | 
|  | 3355 | down = isPointerDown(mCurrentRawState.buttonState); | 
|  | 3356 | hovering = !down; | 
|  | 3357 |  | 
|  | 3358 | float x, y; | 
|  | 3359 | mPointerController->getPosition(&x, &y); | 
|  | 3360 | mPointerSimple.currentCoords.copyFrom( | 
|  | 3361 | mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]); | 
|  | 3362 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); | 
|  | 3363 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); | 
|  | 3364 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, | 
|  | 3365 | hovering ? 0.0f : 1.0f); | 
|  | 3366 | mPointerSimple.currentProperties.id = 0; | 
|  | 3367 | mPointerSimple.currentProperties.toolType = | 
|  | 3368 | mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType; | 
|  | 3369 | } else { | 
|  | 3370 | mPointerVelocityControl.reset(); | 
|  | 3371 |  | 
|  | 3372 | down = false; | 
|  | 3373 | hovering = false; | 
|  | 3374 | } | 
|  | 3375 |  | 
|  | 3376 | dispatchPointerSimple(when, policyFlags, down, hovering); | 
|  | 3377 | } | 
|  | 3378 |  | 
|  | 3379 | void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) { | 
|  | 3380 | abortPointerSimple(when, policyFlags); | 
|  | 3381 |  | 
|  | 3382 | mPointerVelocityControl.reset(); | 
|  | 3383 | } | 
|  | 3384 |  | 
|  | 3385 | void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, bool down, | 
|  | 3386 | bool hovering) { | 
|  | 3387 | int32_t metaState = getContext()->getGlobalMetaState(); | 
|  | 3388 | int32_t displayId = mViewport.displayId; | 
|  | 3389 |  | 
|  | 3390 | if (down || hovering) { | 
|  | 3391 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); | 
|  | 3392 | mPointerController->clearSpots(); | 
|  | 3393 | mPointerController->setButtonState(mCurrentRawState.buttonState); | 
|  | 3394 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); | 
|  | 3395 | } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) { | 
|  | 3396 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); | 
|  | 3397 | } | 
|  | 3398 | displayId = mPointerController->getDisplayId(); | 
|  | 3399 |  | 
|  | 3400 | float xCursorPosition; | 
|  | 3401 | float yCursorPosition; | 
|  | 3402 | mPointerController->getPosition(&xCursorPosition, &yCursorPosition); | 
|  | 3403 |  | 
|  | 3404 | if (mPointerSimple.down && !down) { | 
|  | 3405 | mPointerSimple.down = false; | 
|  | 3406 |  | 
|  | 3407 | // Send up. | 
|  | 3408 | NotifyMotionArgs args(mContext->getNextSequenceNum(), when, getDeviceId(), mSource, | 
|  | 3409 | displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, 0, metaState, | 
|  | 3410 | mLastRawState.buttonState, MotionClassification::NONE, | 
|  | 3411 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties, | 
|  | 3412 | &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision, | 
|  | 3413 | xCursorPosition, yCursorPosition, mPointerSimple.downTime, | 
|  | 3414 | /* videoFrames */ {}); | 
|  | 3415 | getListener()->notifyMotion(&args); | 
|  | 3416 | } | 
|  | 3417 |  | 
|  | 3418 | if (mPointerSimple.hovering && !hovering) { | 
|  | 3419 | mPointerSimple.hovering = false; | 
|  | 3420 |  | 
|  | 3421 | // Send hover exit. | 
|  | 3422 | NotifyMotionArgs args(mContext->getNextSequenceNum(), when, getDeviceId(), mSource, | 
|  | 3423 | displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, | 
|  | 3424 | metaState, mLastRawState.buttonState, MotionClassification::NONE, | 
|  | 3425 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.lastProperties, | 
|  | 3426 | &mPointerSimple.lastCoords, mOrientedXPrecision, mOrientedYPrecision, | 
|  | 3427 | xCursorPosition, yCursorPosition, mPointerSimple.downTime, | 
|  | 3428 | /* videoFrames */ {}); | 
|  | 3429 | getListener()->notifyMotion(&args); | 
|  | 3430 | } | 
|  | 3431 |  | 
|  | 3432 | if (down) { | 
|  | 3433 | if (!mPointerSimple.down) { | 
|  | 3434 | mPointerSimple.down = true; | 
|  | 3435 | mPointerSimple.downTime = when; | 
|  | 3436 |  | 
|  | 3437 | // Send down. | 
|  | 3438 | NotifyMotionArgs args(mContext->getNextSequenceNum(), when, getDeviceId(), mSource, | 
|  | 3439 | displayId, policyFlags, AMOTION_EVENT_ACTION_DOWN, 0, 0, | 
|  | 3440 | metaState, mCurrentRawState.buttonState, | 
|  | 3441 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, | 
|  | 3442 | &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, | 
|  | 3443 | mOrientedXPrecision, mOrientedYPrecision, xCursorPosition, | 
|  | 3444 | yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {}); | 
|  | 3445 | getListener()->notifyMotion(&args); | 
|  | 3446 | } | 
|  | 3447 |  | 
|  | 3448 | // Send move. | 
|  | 3449 | NotifyMotionArgs args(mContext->getNextSequenceNum(), when, getDeviceId(), mSource, | 
|  | 3450 | displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, | 
|  | 3451 | mCurrentRawState.buttonState, MotionClassification::NONE, | 
|  | 3452 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties, | 
|  | 3453 | &mPointerSimple.currentCoords, mOrientedXPrecision, | 
|  | 3454 | mOrientedYPrecision, xCursorPosition, yCursorPosition, | 
|  | 3455 | mPointerSimple.downTime, /* videoFrames */ {}); | 
|  | 3456 | getListener()->notifyMotion(&args); | 
|  | 3457 | } | 
|  | 3458 |  | 
|  | 3459 | if (hovering) { | 
|  | 3460 | if (!mPointerSimple.hovering) { | 
|  | 3461 | mPointerSimple.hovering = true; | 
|  | 3462 |  | 
|  | 3463 | // Send hover enter. | 
|  | 3464 | NotifyMotionArgs args(mContext->getNextSequenceNum(), when, getDeviceId(), mSource, | 
|  | 3465 | displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, | 
|  | 3466 | metaState, mCurrentRawState.buttonState, | 
|  | 3467 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, | 
|  | 3468 | &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, | 
|  | 3469 | mOrientedXPrecision, mOrientedYPrecision, xCursorPosition, | 
|  | 3470 | yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {}); | 
|  | 3471 | getListener()->notifyMotion(&args); | 
|  | 3472 | } | 
|  | 3473 |  | 
|  | 3474 | // Send hover move. | 
|  | 3475 | NotifyMotionArgs args(mContext->getNextSequenceNum(), when, getDeviceId(), mSource, | 
|  | 3476 | displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, | 
|  | 3477 | metaState, mCurrentRawState.buttonState, MotionClassification::NONE, | 
|  | 3478 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties, | 
|  | 3479 | &mPointerSimple.currentCoords, mOrientedXPrecision, | 
|  | 3480 | mOrientedYPrecision, xCursorPosition, yCursorPosition, | 
|  | 3481 | mPointerSimple.downTime, /* videoFrames */ {}); | 
|  | 3482 | getListener()->notifyMotion(&args); | 
|  | 3483 | } | 
|  | 3484 |  | 
|  | 3485 | if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) { | 
|  | 3486 | float vscroll = mCurrentRawState.rawVScroll; | 
|  | 3487 | float hscroll = mCurrentRawState.rawHScroll; | 
|  | 3488 | mWheelYVelocityControl.move(when, nullptr, &vscroll); | 
|  | 3489 | mWheelXVelocityControl.move(when, &hscroll, nullptr); | 
|  | 3490 |  | 
|  | 3491 | // Send scroll. | 
|  | 3492 | PointerCoords pointerCoords; | 
|  | 3493 | pointerCoords.copyFrom(mPointerSimple.currentCoords); | 
|  | 3494 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); | 
|  | 3495 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); | 
|  | 3496 |  | 
|  | 3497 | NotifyMotionArgs args(mContext->getNextSequenceNum(), when, getDeviceId(), mSource, | 
|  | 3498 | displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState, | 
|  | 3499 | mCurrentRawState.buttonState, MotionClassification::NONE, | 
|  | 3500 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &mPointerSimple.currentProperties, | 
|  | 3501 | &pointerCoords, mOrientedXPrecision, mOrientedYPrecision, | 
|  | 3502 | xCursorPosition, yCursorPosition, mPointerSimple.downTime, | 
|  | 3503 | /* videoFrames */ {}); | 
|  | 3504 | getListener()->notifyMotion(&args); | 
|  | 3505 | } | 
|  | 3506 |  | 
|  | 3507 | // Save state. | 
|  | 3508 | if (down || hovering) { | 
|  | 3509 | mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords); | 
|  | 3510 | mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties); | 
|  | 3511 | } else { | 
|  | 3512 | mPointerSimple.reset(); | 
|  | 3513 | } | 
|  | 3514 | } | 
|  | 3515 |  | 
|  | 3516 | void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) { | 
|  | 3517 | mPointerSimple.currentCoords.clear(); | 
|  | 3518 | mPointerSimple.currentProperties.clear(); | 
|  | 3519 |  | 
|  | 3520 | dispatchPointerSimple(when, policyFlags, false, false); | 
|  | 3521 | } | 
|  | 3522 |  | 
|  | 3523 | void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, | 
|  | 3524 | int32_t action, int32_t actionButton, int32_t flags, | 
|  | 3525 | int32_t metaState, int32_t buttonState, int32_t edgeFlags, | 
|  | 3526 | const PointerProperties* properties, | 
|  | 3527 | const PointerCoords* coords, const uint32_t* idToIndex, | 
|  | 3528 | BitSet32 idBits, int32_t changedId, float xPrecision, | 
|  | 3529 | float yPrecision, nsecs_t downTime) { | 
|  | 3530 | PointerCoords pointerCoords[MAX_POINTERS]; | 
|  | 3531 | PointerProperties pointerProperties[MAX_POINTERS]; | 
|  | 3532 | uint32_t pointerCount = 0; | 
|  | 3533 | while (!idBits.isEmpty()) { | 
|  | 3534 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 3535 | uint32_t index = idToIndex[id]; | 
|  | 3536 | pointerProperties[pointerCount].copyFrom(properties[index]); | 
|  | 3537 | pointerCoords[pointerCount].copyFrom(coords[index]); | 
|  | 3538 |  | 
|  | 3539 | if (changedId >= 0 && id == uint32_t(changedId)) { | 
|  | 3540 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; | 
|  | 3541 | } | 
|  | 3542 |  | 
|  | 3543 | pointerCount += 1; | 
|  | 3544 | } | 
|  | 3545 |  | 
|  | 3546 | ALOG_ASSERT(pointerCount != 0); | 
|  | 3547 |  | 
|  | 3548 | if (changedId >= 0 && pointerCount == 1) { | 
|  | 3549 | // Replace initial down and final up action. | 
|  | 3550 | // We can compare the action without masking off the changed pointer index | 
|  | 3551 | // because we know the index is 0. | 
|  | 3552 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { | 
|  | 3553 | action = AMOTION_EVENT_ACTION_DOWN; | 
|  | 3554 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { | 
|  | 3555 | action = AMOTION_EVENT_ACTION_UP; | 
|  | 3556 | } else { | 
|  | 3557 | // Can't happen. | 
|  | 3558 | ALOG_ASSERT(false); | 
|  | 3559 | } | 
|  | 3560 | } | 
|  | 3561 | float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION; | 
|  | 3562 | float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION; | 
|  | 3563 | if (mDeviceMode == DEVICE_MODE_POINTER) { | 
|  | 3564 | mPointerController->getPosition(&xCursorPosition, &yCursorPosition); | 
|  | 3565 | } | 
|  | 3566 | const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE); | 
|  | 3567 | const int32_t deviceId = getDeviceId(); | 
|  | 3568 | std::vector<TouchVideoFrame> frames = mDevice->getEventHub()->getVideoFrames(deviceId); | 
|  | 3569 | std::for_each(frames.begin(), frames.end(), | 
|  | 3570 | [this](TouchVideoFrame& frame) { frame.rotate(this->mSurfaceOrientation); }); | 
|  | 3571 | NotifyMotionArgs args(mContext->getNextSequenceNum(), when, deviceId, source, displayId, | 
|  | 3572 | policyFlags, action, actionButton, flags, metaState, buttonState, | 
|  | 3573 | MotionClassification::NONE, edgeFlags, pointerCount, pointerProperties, | 
|  | 3574 | pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition, | 
|  | 3575 | downTime, std::move(frames)); | 
|  | 3576 | getListener()->notifyMotion(&args); | 
|  | 3577 | } | 
|  | 3578 |  | 
|  | 3579 | bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties, | 
|  | 3580 | const PointerCoords* inCoords, | 
|  | 3581 | const uint32_t* inIdToIndex, | 
|  | 3582 | PointerProperties* outProperties, | 
|  | 3583 | PointerCoords* outCoords, const uint32_t* outIdToIndex, | 
|  | 3584 | BitSet32 idBits) const { | 
|  | 3585 | bool changed = false; | 
|  | 3586 | while (!idBits.isEmpty()) { | 
|  | 3587 | uint32_t id = idBits.clearFirstMarkedBit(); | 
|  | 3588 | uint32_t inIndex = inIdToIndex[id]; | 
|  | 3589 | uint32_t outIndex = outIdToIndex[id]; | 
|  | 3590 |  | 
|  | 3591 | const PointerProperties& curInProperties = inProperties[inIndex]; | 
|  | 3592 | const PointerCoords& curInCoords = inCoords[inIndex]; | 
|  | 3593 | PointerProperties& curOutProperties = outProperties[outIndex]; | 
|  | 3594 | PointerCoords& curOutCoords = outCoords[outIndex]; | 
|  | 3595 |  | 
|  | 3596 | if (curInProperties != curOutProperties) { | 
|  | 3597 | curOutProperties.copyFrom(curInProperties); | 
|  | 3598 | changed = true; | 
|  | 3599 | } | 
|  | 3600 |  | 
|  | 3601 | if (curInCoords != curOutCoords) { | 
|  | 3602 | curOutCoords.copyFrom(curInCoords); | 
|  | 3603 | changed = true; | 
|  | 3604 | } | 
|  | 3605 | } | 
|  | 3606 | return changed; | 
|  | 3607 | } | 
|  | 3608 |  | 
|  | 3609 | void TouchInputMapper::fadePointer() { | 
|  | 3610 | if (mPointerController != nullptr) { | 
|  | 3611 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); | 
|  | 3612 | } | 
|  | 3613 | } | 
|  | 3614 |  | 
|  | 3615 | void TouchInputMapper::cancelTouch(nsecs_t when) { | 
|  | 3616 | abortPointerUsage(when, 0 /*policyFlags*/); | 
|  | 3617 | abortTouches(when, 0 /* policyFlags*/); | 
|  | 3618 | } | 
|  | 3619 |  | 
|  | 3620 | bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) { | 
|  | 3621 | const float scaledX = x * mXScale; | 
|  | 3622 | const float scaledY = y * mYScale; | 
|  | 3623 | return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue && | 
| Arthur Hung | 6cd19a4 | 2019-08-30 19:04:12 +0800 | [diff] [blame] | 3624 | scaledX >= mSurfaceLeft && scaledX <= mSurfaceLeft + mSurfaceWidth && | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3625 | y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue && | 
| Arthur Hung | 6cd19a4 | 2019-08-30 19:04:12 +0800 | [diff] [blame] | 3626 | scaledY >= mSurfaceTop && scaledY <= mSurfaceTop + mSurfaceHeight; | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3627 | } | 
|  | 3628 |  | 
|  | 3629 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) { | 
|  | 3630 | for (const VirtualKey& virtualKey : mVirtualKeys) { | 
|  | 3631 | #if DEBUG_VIRTUAL_KEYS | 
|  | 3632 | ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " | 
|  | 3633 | "left=%d, top=%d, right=%d, bottom=%d", | 
|  | 3634 | x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft, virtualKey.hitTop, | 
|  | 3635 | virtualKey.hitRight, virtualKey.hitBottom); | 
|  | 3636 | #endif | 
|  | 3637 |  | 
|  | 3638 | if (virtualKey.isHit(x, y)) { | 
|  | 3639 | return &virtualKey; | 
|  | 3640 | } | 
|  | 3641 | } | 
|  | 3642 |  | 
|  | 3643 | return nullptr; | 
|  | 3644 | } | 
|  | 3645 |  | 
|  | 3646 | void TouchInputMapper::assignPointerIds(const RawState* last, RawState* current) { | 
|  | 3647 | uint32_t currentPointerCount = current->rawPointerData.pointerCount; | 
|  | 3648 | uint32_t lastPointerCount = last->rawPointerData.pointerCount; | 
|  | 3649 |  | 
|  | 3650 | current->rawPointerData.clearIdBits(); | 
|  | 3651 |  | 
|  | 3652 | if (currentPointerCount == 0) { | 
|  | 3653 | // No pointers to assign. | 
|  | 3654 | return; | 
|  | 3655 | } | 
|  | 3656 |  | 
|  | 3657 | if (lastPointerCount == 0) { | 
|  | 3658 | // All pointers are new. | 
|  | 3659 | for (uint32_t i = 0; i < currentPointerCount; i++) { | 
|  | 3660 | uint32_t id = i; | 
|  | 3661 | current->rawPointerData.pointers[i].id = id; | 
|  | 3662 | current->rawPointerData.idToIndex[id] = i; | 
|  | 3663 | current->rawPointerData.markIdBit(id, current->rawPointerData.isHovering(i)); | 
|  | 3664 | } | 
|  | 3665 | return; | 
|  | 3666 | } | 
|  | 3667 |  | 
|  | 3668 | if (currentPointerCount == 1 && lastPointerCount == 1 && | 
|  | 3669 | current->rawPointerData.pointers[0].toolType == last->rawPointerData.pointers[0].toolType) { | 
|  | 3670 | // Only one pointer and no change in count so it must have the same id as before. | 
|  | 3671 | uint32_t id = last->rawPointerData.pointers[0].id; | 
|  | 3672 | current->rawPointerData.pointers[0].id = id; | 
|  | 3673 | current->rawPointerData.idToIndex[id] = 0; | 
|  | 3674 | current->rawPointerData.markIdBit(id, current->rawPointerData.isHovering(0)); | 
|  | 3675 | return; | 
|  | 3676 | } | 
|  | 3677 |  | 
|  | 3678 | // General case. | 
|  | 3679 | // We build a heap of squared euclidean distances between current and last pointers | 
|  | 3680 | // associated with the current and last pointer indices.  Then, we find the best | 
|  | 3681 | // match (by distance) for each current pointer. | 
|  | 3682 | // The pointers must have the same tool type but it is possible for them to | 
|  | 3683 | // transition from hovering to touching or vice-versa while retaining the same id. | 
|  | 3684 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; | 
|  | 3685 |  | 
|  | 3686 | uint32_t heapSize = 0; | 
|  | 3687 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; | 
|  | 3688 | currentPointerIndex++) { | 
|  | 3689 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; | 
|  | 3690 | lastPointerIndex++) { | 
|  | 3691 | const RawPointerData::Pointer& currentPointer = | 
|  | 3692 | current->rawPointerData.pointers[currentPointerIndex]; | 
|  | 3693 | const RawPointerData::Pointer& lastPointer = | 
|  | 3694 | last->rawPointerData.pointers[lastPointerIndex]; | 
|  | 3695 | if (currentPointer.toolType == lastPointer.toolType) { | 
|  | 3696 | int64_t deltaX = currentPointer.x - lastPointer.x; | 
|  | 3697 | int64_t deltaY = currentPointer.y - lastPointer.y; | 
|  | 3698 |  | 
|  | 3699 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); | 
|  | 3700 |  | 
|  | 3701 | // Insert new element into the heap (sift up). | 
|  | 3702 | heap[heapSize].currentPointerIndex = currentPointerIndex; | 
|  | 3703 | heap[heapSize].lastPointerIndex = lastPointerIndex; | 
|  | 3704 | heap[heapSize].distance = distance; | 
|  | 3705 | heapSize += 1; | 
|  | 3706 | } | 
|  | 3707 | } | 
|  | 3708 | } | 
|  | 3709 |  | 
|  | 3710 | // Heapify | 
|  | 3711 | for (uint32_t startIndex = heapSize / 2; startIndex != 0;) { | 
|  | 3712 | startIndex -= 1; | 
|  | 3713 | for (uint32_t parentIndex = startIndex;;) { | 
|  | 3714 | uint32_t childIndex = parentIndex * 2 + 1; | 
|  | 3715 | if (childIndex >= heapSize) { | 
|  | 3716 | break; | 
|  | 3717 | } | 
|  | 3718 |  | 
|  | 3719 | if (childIndex + 1 < heapSize && | 
|  | 3720 | heap[childIndex + 1].distance < heap[childIndex].distance) { | 
|  | 3721 | childIndex += 1; | 
|  | 3722 | } | 
|  | 3723 |  | 
|  | 3724 | if (heap[parentIndex].distance <= heap[childIndex].distance) { | 
|  | 3725 | break; | 
|  | 3726 | } | 
|  | 3727 |  | 
|  | 3728 | swap(heap[parentIndex], heap[childIndex]); | 
|  | 3729 | parentIndex = childIndex; | 
|  | 3730 | } | 
|  | 3731 | } | 
|  | 3732 |  | 
|  | 3733 | #if DEBUG_POINTER_ASSIGNMENT | 
|  | 3734 | ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize); | 
|  | 3735 | for (size_t i = 0; i < heapSize; i++) { | 
|  | 3736 | ALOGD("  heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i, | 
|  | 3737 | heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance); | 
|  | 3738 | } | 
|  | 3739 | #endif | 
|  | 3740 |  | 
|  | 3741 | // Pull matches out by increasing order of distance. | 
|  | 3742 | // To avoid reassigning pointers that have already been matched, the loop keeps track | 
|  | 3743 | // of which last and current pointers have been matched using the matchedXXXBits variables. | 
|  | 3744 | // It also tracks the used pointer id bits. | 
|  | 3745 | BitSet32 matchedLastBits(0); | 
|  | 3746 | BitSet32 matchedCurrentBits(0); | 
|  | 3747 | BitSet32 usedIdBits(0); | 
|  | 3748 | bool first = true; | 
|  | 3749 | for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) { | 
|  | 3750 | while (heapSize > 0) { | 
|  | 3751 | if (first) { | 
|  | 3752 | // The first time through the loop, we just consume the root element of | 
|  | 3753 | // the heap (the one with smallest distance). | 
|  | 3754 | first = false; | 
|  | 3755 | } else { | 
|  | 3756 | // Previous iterations consumed the root element of the heap. | 
|  | 3757 | // Pop root element off of the heap (sift down). | 
|  | 3758 | heap[0] = heap[heapSize]; | 
|  | 3759 | for (uint32_t parentIndex = 0;;) { | 
|  | 3760 | uint32_t childIndex = parentIndex * 2 + 1; | 
|  | 3761 | if (childIndex >= heapSize) { | 
|  | 3762 | break; | 
|  | 3763 | } | 
|  | 3764 |  | 
|  | 3765 | if (childIndex + 1 < heapSize && | 
|  | 3766 | heap[childIndex + 1].distance < heap[childIndex].distance) { | 
|  | 3767 | childIndex += 1; | 
|  | 3768 | } | 
|  | 3769 |  | 
|  | 3770 | if (heap[parentIndex].distance <= heap[childIndex].distance) { | 
|  | 3771 | break; | 
|  | 3772 | } | 
|  | 3773 |  | 
|  | 3774 | swap(heap[parentIndex], heap[childIndex]); | 
|  | 3775 | parentIndex = childIndex; | 
|  | 3776 | } | 
|  | 3777 |  | 
|  | 3778 | #if DEBUG_POINTER_ASSIGNMENT | 
|  | 3779 | ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize); | 
|  | 3780 | for (size_t i = 0; i < heapSize; i++) { | 
|  | 3781 | ALOGD("  heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i, | 
|  | 3782 | heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance); | 
|  | 3783 | } | 
|  | 3784 | #endif | 
|  | 3785 | } | 
|  | 3786 |  | 
|  | 3787 | heapSize -= 1; | 
|  | 3788 |  | 
|  | 3789 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; | 
|  | 3790 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched | 
|  | 3791 |  | 
|  | 3792 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; | 
|  | 3793 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched | 
|  | 3794 |  | 
|  | 3795 | matchedCurrentBits.markBit(currentPointerIndex); | 
|  | 3796 | matchedLastBits.markBit(lastPointerIndex); | 
|  | 3797 |  | 
|  | 3798 | uint32_t id = last->rawPointerData.pointers[lastPointerIndex].id; | 
|  | 3799 | current->rawPointerData.pointers[currentPointerIndex].id = id; | 
|  | 3800 | current->rawPointerData.idToIndex[id] = currentPointerIndex; | 
|  | 3801 | current->rawPointerData.markIdBit(id, | 
|  | 3802 | current->rawPointerData.isHovering( | 
|  | 3803 | currentPointerIndex)); | 
|  | 3804 | usedIdBits.markBit(id); | 
|  | 3805 |  | 
|  | 3806 | #if DEBUG_POINTER_ASSIGNMENT | 
|  | 3807 | ALOGD("assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32 | 
|  | 3808 | ", distance=%" PRIu64, | 
|  | 3809 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); | 
|  | 3810 | #endif | 
|  | 3811 | break; | 
|  | 3812 | } | 
|  | 3813 | } | 
|  | 3814 |  | 
|  | 3815 | // Assign fresh ids to pointers that were not matched in the process. | 
|  | 3816 | for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) { | 
|  | 3817 | uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit(); | 
|  | 3818 | uint32_t id = usedIdBits.markFirstUnmarkedBit(); | 
|  | 3819 |  | 
|  | 3820 | current->rawPointerData.pointers[currentPointerIndex].id = id; | 
|  | 3821 | current->rawPointerData.idToIndex[id] = currentPointerIndex; | 
|  | 3822 | current->rawPointerData.markIdBit(id, | 
|  | 3823 | current->rawPointerData.isHovering(currentPointerIndex)); | 
|  | 3824 |  | 
|  | 3825 | #if DEBUG_POINTER_ASSIGNMENT | 
|  | 3826 | ALOGD("assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex, id); | 
|  | 3827 | #endif | 
|  | 3828 | } | 
|  | 3829 | } | 
|  | 3830 |  | 
|  | 3831 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { | 
|  | 3832 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) { | 
|  | 3833 | return AKEY_STATE_VIRTUAL; | 
|  | 3834 | } | 
|  | 3835 |  | 
|  | 3836 | for (const VirtualKey& virtualKey : mVirtualKeys) { | 
|  | 3837 | if (virtualKey.keyCode == keyCode) { | 
|  | 3838 | return AKEY_STATE_UP; | 
|  | 3839 | } | 
|  | 3840 | } | 
|  | 3841 |  | 
|  | 3842 | return AKEY_STATE_UNKNOWN; | 
|  | 3843 | } | 
|  | 3844 |  | 
|  | 3845 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { | 
|  | 3846 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) { | 
|  | 3847 | return AKEY_STATE_VIRTUAL; | 
|  | 3848 | } | 
|  | 3849 |  | 
|  | 3850 | for (const VirtualKey& virtualKey : mVirtualKeys) { | 
|  | 3851 | if (virtualKey.scanCode == scanCode) { | 
|  | 3852 | return AKEY_STATE_UP; | 
|  | 3853 | } | 
|  | 3854 | } | 
|  | 3855 |  | 
|  | 3856 | return AKEY_STATE_UNKNOWN; | 
|  | 3857 | } | 
|  | 3858 |  | 
|  | 3859 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, | 
|  | 3860 | const int32_t* keyCodes, uint8_t* outFlags) { | 
|  | 3861 | for (const VirtualKey& virtualKey : mVirtualKeys) { | 
|  | 3862 | for (size_t i = 0; i < numCodes; i++) { | 
|  | 3863 | if (virtualKey.keyCode == keyCodes[i]) { | 
|  | 3864 | outFlags[i] = 1; | 
|  | 3865 | } | 
|  | 3866 | } | 
|  | 3867 | } | 
|  | 3868 |  | 
|  | 3869 | return true; | 
|  | 3870 | } | 
|  | 3871 |  | 
|  | 3872 | std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() { | 
|  | 3873 | if (mParameters.hasAssociatedDisplay) { | 
|  | 3874 | if (mDeviceMode == DEVICE_MODE_POINTER) { | 
|  | 3875 | return std::make_optional(mPointerController->getDisplayId()); | 
|  | 3876 | } else { | 
|  | 3877 | return std::make_optional(mViewport.displayId); | 
|  | 3878 | } | 
|  | 3879 | } | 
|  | 3880 | return std::nullopt; | 
|  | 3881 | } | 
|  | 3882 |  | 
|  | 3883 | } // namespace android |