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