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