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