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